onChange只会偶尔触发一次

时间:2012-10-18 12:51:13

标签: javascript jquery onchange

我有以下问题: 我有一个包含2个输入元素的表。如果其中一个比另一个大,则应该根据比较对整行进行着色。我使用onchange事件触发此操作,并在加载站点时第一次触发。这些过程运行正常,但onchange事件只在4次更改中的一次触发。

表语句如下:

<div class="grid">
 <table id="table" class="tableRegion w100">
  <thead style="text-align:left">
   <tr>
    <fmt:bundle basename="res/web/ec_resources">
     <td class="tableHeader">
    ...
     </td> ...
    </fmt:bundle>
   </tr>
  </thead>      
  <tbody>
   <c:set var="i" value="0"/>
   <c:forEach var="participation" items="${someForm.list}">
     <tr id="participationRow${i}">

在这里,表格的代码&gt; tr> TD:

<td class="right bottom nowrap">
 <div>
  <c:out value="${someForm.event.currency.code}"/>
  <fmt:formatNumber maxFractionDigits="2" minFractionDigits="2" var="ovFee" value="${overallFee}" />
  <c:if test="${someForm.array[i].feeDebit != 0.0}">
   <spring:input class="right debit" path="array[${i}].feeDebit" maxlength="7" size="6" onchange="isPaid(${i});" />
  </c:if><c:if test="${someForm.array[i].feeDebit == 0.0}">
   <spring:input class="right debit" path="array[${i}].feeDebit" maxlength="7" size="6" onchange="isPaid(${i});" value="${ovFee}"/>
  </c:if>
 </div>
</td>

<td class="right bottom nowrap">
 <div class="padT5"><c:out value="${someForm.event.currency.code}"/> <spring:input class="right paid" path="array[${i}].feePaid" maxlength="7" size="6" onchange="isPaid(${i});"/></div>
</td>

被称为skript如下:

$(document).ready(function(){
    $('#table > tbody > tr').each(function(i) {
        var paid = parseFloat($(this).find('input.paid').val());
        var debit = parseFloat($(this).find('input.debit').val());
        if (paid == debit)
            $('#participationRow'+i).addClass("green");
        else if (paid > debit)
            $('#participationRow'+i).addClass("yellow");
        }
    );
});

function isPaid(i){
    var paid = parseFloat($('#participationRow'+i).find('input.paid').val());
    var debit = parseFloat($('#participationRow'+i).find('input.debit').val());
    if (paid == debit)
        $('#participationRow'+i).addClass("green");
    else if (paid > debit)
        $('#participationRow'+i).addClass("yellow");

}

偶尔触发事件的原因是什么?我用jQuery和onclick进行了交叉检查。他们都只是偶尔触发一次。无论我是通过点击离开还是离开而离开。

3 个答案:

答案 0 :(得分:1)

当应该运行时,您的代码缺少触发器的任何定义。

第一部分(.each()块)仅在$(document).ready();上执行,这意味着每页加载时执行一次(文档加载完成后)。

isPaid()函数只不过是一个函数,我没有看到它被调用的位置,因为它在当前的代码片段中不存在。

您的代码本身似乎没问题,但它只是没有任何形式的触发器。

您会期待如下:

$("input").change(function() {

    var the_row = $(this).parent().parent(); //finds the <tr> it belongs to.
    var i = the_row.attr("id").replace("participationRow","");

    isPaid(i);
});

有很多方法可以改进,但这是您在代码中缺少的重要关键。

答案 1 :(得分:1)

    function highlightIfPaid(i){
        var paid = parseFloat($('#participationRow'+i).find('input.paid').val());
        var debit = parseFloat($('#participationRow'+i).find('input.debit').val());
        if (paid == debit)
            $('#participationRow'+i).addClass("green");
        else if (paid > debit)
            $('#participationRow'+i).addClass("yellow");
    }

    $(document).ready(function(){
        //just use jquery for binding to change event,
        //my guess is that Spring doesn't have a value for i at the point and jquery is not loaded by then so $ is undefined
        $('input.debit').change(function(){
            //you need to get the index i based on your table structure
            //I assumed this is just the index of the row
            //I get this method from this other post http://stackoverflow.com/questions/1620391/find-table-row-index-using-jquery
            var i = $(this).closest('tr').prevAll().length;
            highlightIfPaid(i);
        });

//Dry: highlight your rows on first load
$('#table > tbody > tr').each(highlightIfPaid);
    });

答案 2 :(得分:0)

好的......看了一会儿,在@kabaros和@Flater的帮助下,我发现以下内容有效:

$(document).ready(function(){
 $('input.paid').change(function(){
  var i = $(this).closest('tr')[0].sectionRowIndex;
  isPaid(i);
 });

解决方案是触发的$('input.paid'),而不是$('input')。计算行也不是那么容易,但上面的代码对行进行了计算。不幸的是,onchange字段中的input只会偶尔触发一次!

相关问题