Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.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:checkbox_Jquery - Fatal编程技术网

jQuery:checkbox

jQuery:checkbox,jquery,Jquery,我的代码有什么问题,我没有得到任何值: <script> $(document).ready(function() { $("input[type=checkbox][checked]").each(function(event){ var get = $("input[@name=\'checkbox_pref\']:checked").val(); $("#result").html("&id

我的代码有什么问题,我没有得到任何值:

<script>
    $(document).ready(function()
    {
        $("input[type=checkbox][checked]").each(function(event){
            var get = $("input[@name=\'checkbox_pref\']:checked").val();
            $("#result").html("&id=" + get);
        });
    });
</script>
</head>                                                                 
<body>                                                                
<input type="checkbox" name="checkbox_pref" value = "1"/>
<input type="checkbox" name="checkbox_pref" value = "2"/>   
<input type="checkbox" name="checkbox_pref" value = "3"/>   
<div id="result">result ...</div>

您不需要转义单引号或@符号。使用此行:

$(document).ready(function()
{
    $("input[type=checkbox][checked]").each(function(event){
        var get = $("input[name='checkbox_pref']:checked").val();
        $("#result").html("&id=" + get);
    });
});

这取决于你想做什么,但应该更多地遵循以下原则:

$(document).ready(function()
{
    $("input[type=checkbox]").change(function(event){
        $("#result").html("&id=" + this.value);
    });
});

我自己刚刚检查了一种非常类似的功能,下面是我的测试。仅供参考


在我看来,您正在为加载时选中的复选框执行该功能。它们中有没有自动勾选的?转义单引号没有错。编辑:我看到你更新了答案,删除了@符号。。。我刚刚在jsFiddle.net上检查了您的解决方案,但它似乎不起作用。我同意,虽然我会做$'input:checkbox'谢谢,但如何获得所有选定的值,不只是最后一个?