在这种情况下,更改事件不起作用

时间:2011-11-09 11:23:19

标签: jquery

<table>
<tr>
<td><input type='checkbox'><span>Text1</span><input type='text'></td>
</tr>
<tr>
<td><input type='checkbox'><span>Text2</span></td>
</tr>
<tr>
<td><input type='checkbox'><span>Text3</span></td>
</tr>
<tr>
<td><input type='radio'><span>None of all</span></td>
</tr>
</table>

以下是我的jQuery代码..

$(':checkbox').live('change',function()
            {
                $(':radio').prop('checked',!$(this).prop('checked'));
            });
$(':radio').live('change',function()
            {
                $(':checkbox').prop('checked',!$(this).prop('checked'));
            });

$('table :text').live('keyup', function() {
var um = $(this).closest('td').find('input');
if ($(this).val().length > 0) {
    um.prop('checked', true);
}
else {
    um.prop('checked', false);
}
});

以上代码的作用是

 1. Checkboxes and Radio button are mutually exclusive
 2. On change of a textbox automatically the checkbox inside its parent will checked/unchecked.

但是

我关注的是当我第一次点击单选按钮时,如果我在文本框中更改文本,则会选中复选框,但单选按钮仍未选中。为什么会发生这种情况..请帮助我这个..

3 个答案:

答案 0 :(得分:3)

与自动完成插件存在相同的问题,其中选择的更改了文本元素但未触发更改事件。你可以添加一个

um.trigger("change");
在你的if / else

之后

答案 1 :(得分:2)

最简单的方法是在更改该复选框的状态时触发更改事件:

if ($(this).val().length > 0) {
    um.prop('checked', true);
}else{
    um.prop('checked', false);
}
um.change();

答案 2 :(得分:0)

相反,如果更改使用单击

$(':checkbox').live('change',function()  {

希望它能为你效果