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'谢谢,但如何获得所有选定的值,不只是最后一个?