Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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/3/html/77.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
jQuery复选框-解释_Jquery_Html - Fatal编程技术网

jQuery复选框-解释

jQuery复选框-解释,jquery,html,Jquery,Html,我制作了一个小表单,有很多复选框。当我点击CheckAll复选框时,所有复选框都会被自动选中。这是我的代码,但我只从互联网上获取了以下代码。 虽然它可以工作,但由于我是jQuery新手,我不理解这段代码的作用。有人能解释一下吗 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <scri

我制作了一个小表单,有很多复选框。当我点击CheckAll复选框时,所有复选框都会被自动选中。这是我的代码,但我只从互联网上获取了以下代码。 虽然它可以工作,但由于我是jQuery新手,我不理解这段代码的作用。有人能解释一下吗

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#checkAll").change(function () {
    $("input:checkbox").prop('checked', $(this).prop("checked"));
    alert("Yoy have checked all");
});
});
</script>
</head>
<body>
<form>
    <p><label><input type="checkbox" id="checkAll"/> Check all</label></p>

    <fieldset>
        <legend>All Checkboxes</legend>
        <p><label><input type="checkbox" /> Option 1</label></p>
        <p><label><input type="checkbox" /> Option 2</label></p>
        <p><label><input type="checkbox" /> Option 3</label></p>
        <p><label><input type="checkbox" /> Option 4</label></p>
    </fieldset>
</form>
</body>
</html>

以下是带有注释的脚本:

// When document is ready, run function
$(document).ready(function(){

    // When 'change' event is triggered on element with id 'checkAll'
    // run function 
    $("#checkAll").change(function () {

        // Set property 'checked' of all inputs with type 'checkbox'
        // to state that #checkAll input has
        $("input:checkbox").prop('checked', $(this).prop("checked"));

        // Alert 'Yoy have checked all'
        alert("Yoy have checked all");
    });

});
你可以在行动中看到它

如果您对jQuery或JavaScript不是很熟悉,请查看YouTube上的一些初学者教程,或者只是谷歌一下。有很多视频教程解释基本的HTML和jQuery内容,它们应该非常有用

请注意,stackoverflow并不是其他开发人员解释某些代码片段的作用的地方


希望这是有用的:

这是非常简单的,首先在这段代码中,选中第一个复选框时很简单,然后发生更改事件,下一行$input:checkboxselector选择all复选框。 和prop函数检查已检查的属性
$this.propchecked;添加“已选中的项目”复选框,所有复选框均已选中

您的问题是什么?请向我解释脚本在上面的程序$input:checkbox中是如何工作的,它选择checkbox类型的所有输入元素,并简单地向所有元素添加checkbox属性$this.propchecked是检查该复选框是否已选中的一种方法。因此,换句话说,代码将检查复选框是否选中true或false,并使用该结果取消选中或选中复选框。这个例程将为checkbox.jQuery类型的每个输入元素执行。jQuery有一个奇妙的参考,您可以使用它查找任何您不确定的选择器、属性和方法。好的@Jorrex..我正在尝试..再次发送。。