我想将选中的复选框值存储到我的隐藏字段中,但我没有得到值。
我有一个隐藏字段名为Storecheckvalue
,我想将我选中的复选框值以逗号分隔的形式存储到这个隐藏字段中。
这是我的Fiddle:Js Fiddle
您可以创建一个数组,并循环通过所有选中的复选框来更新数组
var values = [];
$('input[type="checkbox"][id^="chkchild"]:checked').each(function(){
values.push($(this).val());
});
alert(values);
工作演示
使用join函数连接值数组:
var values = [];
$('[id^="chk"]:checked:not("#chkAll"):not("[id^="chkParent"]")').each(function() {
values.push($(this).val());
});
alert(values.join());
请参阅:http://jsfiddle.net/1kemunrt/2/
或者对于随机id,使用控件的类
<input class="not" type="checkbox" id="chkAll" onchange="Checkallcheckbox(this)" runat="server" value="Parent" />
<br /> ---------Section 1 -----------
<div id="section1">
<input type="hidden" value="" id="Storecheckvalue" />
<li class="carousel-border">
<input class="not" type="checkbox" id="chkParent1" runat="server" onchange="CheckAllInSection(this)" value="Check All" />
</li>
<li>
<input type="checkbox" id="chkchild1" runat="server" value="Child 1" />
</li>
<li>
<input type="checkbox" id="chkchild2" runat="server" value="Child 2" />
</li>
</div>
---------Section 2 -----------
<div id="section2">
<li class="carousel-border">
<input class="not" type="checkbox" id="chkParent2" runat="server" onchange="CheckAllInSection(this)" value="Check All" />
</li>
<li>
<input type="checkbox" id="chkchild3" runat="server" value="Child 3" />
</li>
<li>
<input type="checkbox" id="chkchild4" runat="server" value="Child 4" />
</li>
</div>
<input type="button" onclick="Savecheckedvalueinhiddenfield()" value="GetCheckedId" />
js:
function Savecheckedvalueinhiddenfield() {
var values = [];
$(':checked:not(".not")').each(function() {
values.push($(this).val());
});
alert(values.join());
}