如何检索jQuery中的复选框值 [英] How to retrieve checkboxes values in jQuery
问题描述
如何使用 jQuery 获取选中的复选框值,并将其放入textarea
< html>
< head>
< / head>
< body>
< div id =c_b>
< input type =checkboxvalue =one_namechecked>
< input type =checkboxvalue =one_name1>
< input type =checkboxvalue =one_name2>
< / div>
< textarea id =t>< / textarea>
< / body>
< / html>
如果 id =c_d
由 Ajax 更新,下面的altCognito代码不起作用。是否有好的解决方案?
更新
以下是的作品。 (见示例)
function updateTextArea(){
var allVals = [];
$('#c_b:checked')每个(function(){
allVals.push($(this).val());
});
$('#t')。val(allVals);
}
$(function(){
$('#c_b input')。click(updateTextArea);
updateTextArea();
});
在几个月后,有人询问如何保持上述工作的另一个问题变化。好吧,解决方案归结为将updateTextArea函数映射到使用css类的泛型,并使用live函数来监视这些更改的DOM。对不起,我现在不想写这个例子。
How to use jQuery to get the checked checkboxes values, and put it into a textarea immediately?
Just like this code:
<html>
<head>
</head>
<body>
<div id="c_b">
<input type="checkbox" value="one_name" checked>
<input type="checkbox" value="one_name1">
<input type="checkbox" value="one_name2">
</div>
<textarea id="t"></textarea>
</body>
</html>
If the id="c_d"
is updated by Ajax, the below of altCognito's code doesn't work. Is there any good solution?
Update
Here's one that works. (see the example)
function updateTextArea() {
var allVals = [];
$('#c_b :checked').each(function() {
allVals.push($(this).val());
});
$('#t').val(allVals);
}
$(function() {
$('#c_b input').click(updateTextArea);
updateTextArea();
});
Some number of months later another question was asked in regards to how to keep the above working if the ID changes. Well, the solution boils down to mapping the updateTextArea function into something generic that uses css classes, and to use the live function to monitor the DOM for those changes. Sorry, I'm not really up to writing the example at the moment.
这篇关于如何检索jQuery中的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!