jQuery只影响while循环中的最后一个结果:MySQL [英] jQuery only affects the last result in a while loop: MySQL

查看:135
本文介绍了jQuery只影响while循环中的最后一个结果:MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在while循环中连续更改每个选中项目的背景颜色.目前,jQuery仅适用于最后一行.我确实在输入id中放入了$i变量,但是老实说,我不确定该怎么做.我尝试了.each函数以及一系列答案堆栈溢出,但是我不知道该如何处理.

I am trying to make the background color change for each checked item in a row in a while loop. Currently the jQuery only works on the last row. I did put an $i variable in the input id, but to be honest, I'm not sure what to do beyond it. I tried this, the .each function, and a bunch of answers on Stack Overflow, but I can't figure out how to take care of this.

这是jQuery:

$(document).ready(function(){
    $('#org[$i]').change(function(){
        if(this.checked){
            $(this).siblings('table').removeClass('unchecked');
            $(this).siblings('table').addClass('checked');
            $(this).parentsUntil('td').removeClass('unchecked');
            $(this).parentsUntil('td').addClass('checked');
        } else {
            $(this).siblings('table').removeClass('checked');
            $(this).siblings('table').addClass('unchecked');
            $(this).parentsUntil('td').removeClass('checked');
            $(this).parentsUntil('td').addClass('unchecked');
        }               
    });
});

这是循环(删除了一些不重要的内容). $i在每个#org中正确迭代(我在Firebug中检查过):

Here is the loop (with some unimportant stuff cut out). $i iterates properly in each #org (I checked in Firebug):

if ($i % 4 == 0) echo ($i > 0? '</tr>' : '') . '<tr>';

echo "<td class=\"unchecked\">
    <div class=\"unchecked\">
        <input id=\"org[".$i."]\" style=\"float:right\" type=\"checkbox\" name=\"org\" value=\"".$row['i_id']."\"/>
        <table class=\"unchecked\">
            //blah, blah, blah
        </table>
    </div>
</td>';                 

if ($i == $countRows - 1)
    echo '</tr>';
$i++;

推荐答案

您的问题是,您试图将像org[1]这样的ID匹配为PHP的输出,但是您正在将jQuery选择器与[$i]一起使用其中-出于某些原因,它不起作用.

Your problem is that you're trying to match an id like org[1] as output by PHP, but you're using a jQuery selector with [$i] in it - that doesn't work, for a few reasons.

起作用的是这样的jQuery选择器:

What will work is a jQuery selector like this:

$('input[id^="org\["]')

这将选择以org[开头的id的输入元素.我在括号的前面放了一个空格,因为jQuery对括号有特殊的含义,而这个转义"就是这种特殊的含义.

This will select input elements with an id that starts with org[. I placed a backspace in front of the bracket because jQuery has a special meaning for brackets, and this "escapes" that special meaning.

这篇关于jQuery只影响while循环中的最后一个结果:MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆