Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在(';更改';)上未按预期工作_Javascript_Jquery - Fatal编程技术网

Javascript 在(';更改';)上未按预期工作

Javascript 在(';更改';)上未按预期工作,javascript,jquery,Javascript,Jquery,下面是我的代码,它只工作一个周期的全选和全选。如果我将语法更改为.change而不是.on('change'),效果很好。甚至.change也可以在jquery的1.8.3版本中使用,但不能在1.8.3版本之后使用 <div id="mainframe" align="center"> <div align="left"><strong>Select All CheckBox Demo.</strong></div> <t

下面是我的代码,它只工作一个周期的全选和全选。如果我将语法更改为
.change
而不是
.on('change')
,效果很好。甚至
.change
也可以在jquery的1.8.3版本中使用,但不能在1.8.3版本之后使用

<div id="mainframe" align="center">
  <div align="left"><strong>Select All CheckBox Demo.</strong></div>
  <table border="0" cellpadding="5" cellspacing="1" class="table">
    <th><input type="checkbox" id="selectall"/></th>
    <th>Programming Language</th>
    <th>Rating</th>
    <tr>
      <td align="center"><input type="checkbox" class="case" name="case" value="1"/></td>
      <td>PHP</td>
      <td>5</td>
    </tr>
    <tr>
      <td align="center"><input type="checkbox" class="case" name="case" value="2"/></td>
      <td>JSP</td>
      <td>4</td>
    </tr>
    <tr>
      <td align="center"><input type="checkbox" class="case" name="case" value="3"/></td>
      <td>ASP</td>
      <td>3</td>
    </tr>
  </table>
</div>

问题在于
attr
的使用方式。改用
prop
简化它

$(function(){
    $("#selectall").on('change',function () {
        $('.case').prop('checked', this.checked);
    });
});

根据评论中的问题进行更新


jquery的1.3.2版不支持
on
以及
prop
。这是一个非常旧的版本,
attr
本身可能会处理特定的属性值(对于元素
属性
)或布尔值(对于元素
属性
),以添加/删除属性值(对于此类属性(
选中
选中
)或根据指定的值类型设置或取消设置元素属性。jquery的更高版本引入了
prop
来设置元素属性,并引入了
attr
来设置元素属性。也许这就是原因。还要注意的是,jquery的1.3.2版本不支持
on

您需要使用

$('.case').attr('checked', 'checked');

此外,您可以在一行代码中完成此操作:

$(function(){
    $("#selectall").on('change',function () {
        $('.case').prop('checked', $(this).is(":checked"));
    });
});

这里有一个JSFIDLE:

如果调用
.change
可以,但是调用
.on(“change”…
不行,为什么不使用有效的方法呢?我很困惑。
.change()
.on('change',…)之间没有区别
因此,我认为一定发生了其他事情。你能创建一个jsFiddle.net示例来说明这个问题吗?我想知道它没有按预期工作的原因。很好的解决方法。你有关于这个问题的(链接)背景信息吗?等等-这与(公认可疑的)处理
.change()有什么关系
.on()
的比较
.attr()
.prop()
之间的区别:也许我在这里遗漏了一些东西,但这仍然不能解释为什么
.change
打开时有效('change'
不支持此功能。@susheel这可能是由于jquery中的内部实现将attr中的布尔值转换为1.3.2版中的添加/删除属性。之后的版本引入了
prop
来分离此功能。另外请注意,1.3.2除了
prop
$('.case').prop('checked', true);
$(function(){
    $("#selectall").on('change',function () {
        $('.case').prop('checked', $(this).is(":checked"));
    });
});