[英]Get checkbox value even if not checked?
Is there a way to get the values of all checkboxes, even if they are not checked? 有没有一种方法可以获取所有复选框的值,即使未选中它们也是如此?
HTML: HTML:
<input type="checkbox" name="color[]" value="1"/>
<input type="checkbox" name="color[]" value="2"/>
...
JS: JS:
$("input[name='color[]']").serialize();
Something like the above, but I want the values even if they are not checked. 类似于上面的内容,但是即使没有选中这些值,我也想要它们。 Perhaps I need to put them into an array first before serialising?
也许我需要在序列化之前先将它们放入数组中?
document.write($('input[name="color[]"]').clone().prop('checked', true).serialize());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <input type="checkbox" name="color[]" value="1" /> <input type="checkbox" name="color[]" value="2" />
The simple solution seems to be missing, so: 简单的解决方案似乎丢失了,所以:
var serializedValues = $('input[name="color[]"]').map(function(i, e) { return encodeURIComponent(e.name) + "=" + encodeURIComponent(e.value); }).get().join("&"); snippet.log(serializedValues);
<input type="checkbox" name="color[]" value="1" /> <input type="checkbox" name="color[]" value="2" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
That's assuming you really want the same output that seralize
would give you. 假设您确实希望通过
seralize
获得相同的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.