Sir, There is 1 checkboxList having 3 items in it and 1 button and 1 label. My objective is to when if does not select any checkbox
item and click on button then there should be display a message "Please select any option".and if we select any checkbox and then click on the button then that checkbox text and value should be display on label.
Html Code:----
<form id="form1" runat="server">
<div>
<b>Select Options</b>
<asp:CheckBoxList ID="chkList1" runat="server">
<asp:ListItem Text="Option1" Value="opt1" />
<asp:ListItem Text="Option2" Value="opt2" />
<asp:ListItem Text="Option3" Value="opt3" />
</asp:CheckBoxList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" /><br />
<asp:Label ID="lblResult" runat="server" />
</div>
</form>
jQuery Code:---
<script src="Scripts/jquery-2.1.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnSubmit").click(function () {
if ($("#chkList1 input:checkbox").is(":checked") == false) {
$("#lblResult").html("<b style='color:red'>Please Select any Option</b>");
return false;
}
else
{
var texts = "";
var values = "";
$("#chkList1 input:checkbox:checked").each(function () {
values += $(this).val() + ",";
texts += $("label=[for=" + $(this).prop("id") + "]").text() + ",";
});
texts = texts.substring(0, texts.length - 1);
values = values.substring(0, values.length - 1);
$("#lblResult").html("<b>Selected Option :</b><br/><b>Texts :</b>" + texts + "<br/><b>Values :</b>" + values);
return false;
}
});
});
</script>
Thanks and Regards,
Krishna Kumar
Thanks and Regards,
Krishna Kumar