In this article I will explain
how to get all checked or selected checkboxes values by name using jquery. We
can also get selected value byusing server side code in asp.net. But jquery is
fast as compared to server side code.
Following method we will use to
get Selected Check box values:
var slvals = []
$('input:checkbox[name=nameofyourcheckbox]:checked').each(function()
{
slvals.push($(this).val())
})
})
Full Source code for Sample
Application:
<html>
<head>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function
() {
$('#btnSubmit').click(function () {
var
slvals = []
$('input:checkbox[name=chkcountry]:checked').each(function () {
slvals.push($(this).val())
})
alert('Selected
Checkbox values are: ' + slvals)
})
});
</script>
</head>
<body>
<center>
<fieldset style="width:350px">
<legend><strong>Get
Selected Checkboxes Value Using Jquery</strong></legend>
<b>Sample checkbox inputs :</b><br />
<input type="checkbox"
name="chkcountry"
id="chkAnil"
value="Anil"
/>Anil<br />
<input type="checkbox"
name="chkcountry"
id="chkAnish"
value="Anish"
/>Anish<br />
<input type="checkbox"
name="chkcountry"
id="chkVijay"
value="Vijay"
/>Vijay<br />
<input type="checkbox"
name="chkcountry"
id="chkManuj"
value="Manuj"
/>Manuj<br />
<button id="btnSubmit">Get
Selected Values</button></fieldset></center>
</body>
</html>
0 comments:
Post a Comment