28415

Javascript/JQuery Textbox Values Calculations

Question:

I was trying to make a simple computation out of these textboxes. <br />This is my code so far:

@foreach (var discount in Model.Discounts) { <div class="editor-label"> <label for="@discount.Key.DiscountCode">@discount.Key.DiscountName</label> </div> <div class="editor-field"> <input class="discountCode" name="discountCode" readonly="true" type="text" /> <a class="remove" data-key="discountCode" href="javascript:void(0);">Remove</a> <a class="edit" data-key="discountCode" href="javascript:void(0);">Edit</a> </div> <br /> } <div class="totaldiscounts"> Total: <input id="total" type="text" /> </div> <script> $('.edit').on('click', function () { var parent = $(this).parent(); var input = parent.find('input'); if (input.attr('readonly') === 'readonly') { $(this).text('Stop Edit'); } else { $(this).text('Edit'); } input.attr('readonly', !input.attr('readonly')); }); $('.remove').on('click', function () { var parent = $(this).parent(); var input = parent.find('input'); input[0].value = 0; $(input).first().blur(); var parent = $(this).closest("div"); var uncle = parent.prev(); parent.remove(); uncle.remove(); }); $('.discountCode').on('blur', function () { var inputs = $(".discountCode"); var val = 0; for (var i = 0; i < inputs.length; i++) { if (!isNaN(inputs[i].value)) { val += +inputs[i].value; } } $('#total').val(val); }); </script>

This code works until I have to change the class name of each textboxes into their actual unique discount codes like this:

<input class="@Model.discount.discountCode" name="@Model.discount.discountCode" readonly="true" type="text" />

So apparently, my javascript no longer works because the classname of each textboxes are now different to each other. <br /> How can I make my javascript work with this setup.<br /> Any help will be greatly appreciated. Thank you!!

Answer1:

Put the additional class name into element(example).

class="unique_class_name shared_class_name"

After all, loop to each class(shared_class_name), to get the value like following code :

$('.shared_class_name').on('blur', function ()....

Recommend

  • How to edit a Word document that opens in read-mode in VBA
  • DependencyObject.AssociatedObject is always null
  • How to getText() from the input field of an angularjs Application
  • Disable Kendo Autocomplete
  • Adding elements to a huge XML file
  • Blackberry - Custom EditField Cursor
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Android full screen on only one activity?
  • Django: Count of Group Elements
  • FFmpeg Conversion Error
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Retrieving value from sql ExecuteScalar()
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Fetching methods from BroadcastReceiver to update UI
  • MySQL WHERE-condition in procedure ignored
  • Symfony2: How to get request parameter
  • what is the difference between the asp.net mvc application and asp.net web application
  • Web-crawler for facebook in python
  • GridView Sorting works once only
  • Matrix multiplication with MKL
  • How to disable jQuery.jplayer autoplay?
  • KeystoneJS: Relationships in Admin UI not updating
  • WPF Applying a trigger on binding failure
  • trying to dynamically update Highchart column chart but series undefined
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • embed rChart in Markdown
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal