简体   繁体   English

一个输入字段值不应大于表的其他字段值

[英]One input field value should not be greater than other field value of table

I am asking this question again because i have a new issue in it. 我再次问这个问题,因为我有一个新问题。 I have two input fields one for advance payment and other for full payment, which are fetched from the database in array. 我有两个输入字段,一个用于预付款,另一个用于全额付款,这些字段是从数组中的数据库中提取的。

If onchange or keyup the advance payment is greater than full payment, Then the advance payment should not be entered or should be equal to full payment. 如果onchangekeyup预付款大于全额付款,则不应输入预付款或应等于全额付款。

i have got the following answer previously which worked for me, But now i have a new issue in it. 我之前得到了以下适用于我的答案,但现在我有一个新问题。 it works with existing data which are fetched from the database in array form. 它适用于以数组形式从数据库中提取的现有数据。 But when i increase a new row with JavaScript it is not working in it. 但是,当我使用JavaScript增加一个新行时,它无法正常工作。

I am trying with this code 我正在尝试使用此代码

 $('.advance').on('change keyup blur', function(e) { id_arr = $(this).attr('id'); id = id_arr.split("_"); var fullPay = $('#fullPayment_' + id[1]).val(); var advancePay = $('#advancePayment_' + id[1]).val(); if ($(this).val() > parseInt(fullPay)) { e.preventDefault(); $(this).val(fullPay); } }); var i = $('table tr').length; $("#newRow").on('click', function() { html = '<tr>'; html += '<td><input type="number" value="15" id="fullPayment_' + i + '"></td>'; html += '<td><input type="number" id="advancePayment_' + i + '" class="advance"></td>'; html += '</tr>'; $('table').append(html); i++; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <input type="number" value="12" id="fullPayment_1"> </td> <td> <input type="number" id="advancePayment_1" class="advance"> </td> </tr> <tr> <td> <input type="number" value="19" id="fullPayment_2"> </td> <td> <input type="number" id="advancePayment_2" class="advance"> </td> </tr> </table> <button type="button" id="newRow"> + Add </button> 

Try changing the event handler to this format: 尝试将事件处理程序更改为以下格式:

$(document).on('change keyup blur','.advance', function(e){... });

More about event delegation here: https://learn.jquery.com/events/event-delegation/ 有关事件委派的更多信息,请访问: https//learn.jquery.com/events/event-delegation/

Demo: http://jsfiddle.net/GCu2D/3643/ 演示: http//jsfiddle.net/GCu2D/3643/

 $(document).on('change keyup blur', '.advance', function(e) { id_arr = $(this).attr('id'); id = id_arr.split("_"); var fullPay = $('#fullPayment_' + id[1]).val(); var advancePay = $('#advancePayment_' + id[1]).val(); if ($(this).val() > parseInt(fullPay)) { e.preventDefault(); $(this).val(fullPay); } }); var i = $('table tr').length; $("#newRow").on('click', function() { html = '<tr>'; html += '<td><input type="number" value="15" id="fullPayment_' + i + '"></td>'; html += '<td><input type="number" id="advancePayment_' + i + '" class="advance"></td>'; html += '</tr>'; $('table').append(html); i++; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td><input type="number" value="12" id="fullPayment_1"></td> <td><input type="number" id="advancePayment_1" class="advance"></td> </tr> <tr> <td><input type="number" value="19" id="fullPayment_2"></td> <td><input type="number" id="advancePayment_2" class="advance"></td> </tr> </table> <button type="button" id="newRow"> + Add </button> 

Your code could be a lot more dynamic and automated. 您的代码可以更加动态和自动化。

Perhaps something like this; 也许这样的事情;

<table>
  <tr>
    <td><input type="number" value="12" id="fullPayment_1" class="fullPayment"></td>
    <td><input type="number" value="0" id="advancePayment_1" class="advance"></td>
  </tr>
  <tr>
    <td><input type="number" value="19" id="fullPayment_2" class="fullPayment"></td>
    <td><input type="number" value="0" id="advancePayment_2" class="advance"></td>
  </tr>
</table>


$(function() {

//Put it on the parent element so new inputs have the binding too
  $('table').on('keyup', '.advance', function() {
  //Get the together belonging advance and fullpayment rows
    var $advance = $(this);
    var $fullPayment = $(this).parents('tr').find('.fullPayment');
    //Pars the values to numerics for a good comparison
    var advanceValue = parseFloat($advance.val());
    var fullValue = parseFloat($fullPayment.val());
    console.log(advanceValue, fullValue)
    if (advanceValue > fullValue) {
      $advance.val(fullValue);
    }

  });

});

See this working JSFiddle 看到这个工作JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM