简体   繁体   English

jQuery each()在IE 7中不起作用

[英]jQuery each() not working in IE 7

I have the following function that works properly in Chrome and Firefox, but not in IE7. 我具有以下功能,该功能可以在Chrome和Firefox中正常运行,但不能在IE7中运行。 The function is supposed to run on every instance of a row in the selected table. 该功能应该在选定表中一行的每个实例上运行。 In IE 7 it only runs once. 在IE 7中,它只能运行一次。

function rowTotalit(targee) {
    var sum = 0;
    var thisRow = targee.closest('tr');
    thisRow.find('td.tmipt input').each(function() {
        var numChk = $(this).val();
        sum = sum + Number(numChk);
        thisRow.find('.total').fadeOut(200, function() {
            $(this).html(sum);
            $(this).fadeIn(500);
            $(this).val(sum);
        });
        thisRow.find('.total').next().val(sum);
    });
}​

Here is the trigger from within another function: 这是另一个函数中的触发器:

for (trs = 1; trs <= tblerows; trs++) {
    $("#testingmsg").append("ARGH!!" + trs);
    rowTotalit($("#timeChart tr:nth-child(" + trs + ")"));
}​

Output of the testingmsg div in Chrome and Firefox is: Chrome和Firefox中的testingmsg div的输出为:

ARGH!!1ARGH!!2ARGH!!3ARGH!!4ARGH!!5ARGH!!6ARGH!!7 ARGH !! 1ARGH !! 2ARGH !! 3ARGH !! 4ARGH !! 5ARGH !! 6ARGH !! 7

In IE7 it's: 在IE7中为:

ARGH!!1ARGH!!2 ARGH !! 1ARGH !! 2

So the output is telling me that the function gets to the each statement within the function and dies in IE. 所以输出告诉我该函数到达该函数内的每个语句并在IE中死亡。 Anyone have any ideas? 有人有想法么?

UPDATE: The function that holds the for loop is triggered by a click event on a button: 更新:保留for循环的函数由按钮上的click事件触发:

function addEmAllUp(){
    var tblerows = ($('#timeChart tr').length - 2);
    alert(tblerows);
    for (trs = 1; trs <=tblerows; trs++){
        $("#testingmsg").append("ARGH!!" + trs);
        rowTotalit($("#timeChart tr:nth-child(" + trs +")"));
    }
}

When I click the button more than once, the function increments by 1. So I press it twice, it goes to the second row, third time gets me the third row, etc. 当我多次单击该按钮时,该功能将增加1。因此,我按了两次,它转到第二行,第三次进入第三行,依此类推。

Here is an example of the table row HTML: 这是表格行HTML的示例:

 <tr class="timerow">
    <td class='projName' >Holiday<br /><span class='small'>999906</span></td>
<td class="tmipt" align="center"><input type="text" class="numbersOnly  alertME"  name="999906[]" value="0.00" /></td>
    <td class="tmipt" align="center"><input type="text" class="numbersOnly  alertME"  name="999906[]" value="0.00" /></td>
    <td class="tmipt" align="center"><input type="text" class="numbersOnly "  name="999906[]" value="0.00" /></td>
    <td class="tmipt" align="center"><input type="text" class="numbersOnly "  name="999906[]" value="0.00" /></td>
    <td class="tmipt" align="center"><input type="text" class="numbersOnly "  name="999906[]" value="0.00" /></td>
    <td class="tmipt" align="center"><input type="text" class="numbersOnly "  name="999906[]" value="0.00" /></td>
    <td class="tmipt" align="center"><input type="text" class="numbersOnly "  name="999906[]" value="0.00" /></td>
    <td align="center"><div id="total_1" class="total">0.00</div><input type="hidden" name="total_999906" class="total" value="0.00" /></td>
    </tr>

Are your table rows <tr> being toggled (shown/hidden)? 您的表行<tr>是否已切换(显示/隐藏)?

IE handles this differently than other browsers, you may want to investigate if that is your issue using a DOM inspector like the one in Chrome (F12). IE与其他浏览器的处理方式不同,您可能希望使用DOM检查器(例如Chrome浏览器(F12))调查这是否是您的问题。

Here's an article that talks about that IE bug, though there are many available. 尽管有很多可用的文章 ,但这是一篇讨论IE错误的文章

Got it!! 得到它了!!

There is another find() statement inside the each() that was killing the each prematurely. each()中还有另一个find()语句,该语句过早地杀死了每个。

thisRow.find('.total').fadeOut(200, function() {
            $(this).html(sum);
            $(this).fadeIn(500);
            $(this).val(sum);
        });
        thisRow.find('.total').next().val(sum);

I just moved the above code out of the each statement and it works properly. 我只是将上面的代码从每个语句中移出,它可以正常工作。

For anyone who may be suffering with the same problem of IE not running your .each() properly, here is how I debugged my problem. 对于任何可能会遇到相同的IE问题的人,IE无法正确运行您的.each(),这就是我调试问题的方式。

if($.browser.msie){
alert(someTestValue);
}

I used the above condition to test out different portions of my code for IE only and that not only helped to narrow down what was wrong, but I was able to keep my working code in tact for FF & Chrome. 我使用上述条件仅测试了IE的代码的不同部分,这不仅有助于缩小问题所在,而且还使FF和Chrome的工作代码保持完整。

My final solution was to acutally use the browser if statement to run a different function for IE and another for FF & Chrome. 我的最终解决方案是手动使用浏览器if语句为IE和FF&Chrome运行另一个功能。

Thanks to those that offered your help. 感谢那些为您提供帮助的人。 Hope this helps someone else. 希望这对其他人有帮助。

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

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