I have the following sample code which contains two checkboxes. When the user hits the Submit button I have some JQuery code which determines how many of the checkboxes have been selected.
<html>
<head>
<title>Test</title>
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script>
$(function () {
$('form').submit(function () {
var fields = $("input[name='group']").serializeArray();
if (fields.length == 0) {
//alert('nothing selected');
$("myText").html("Selected");
}
else {
//alert(fields.length + " items selected");
$("myText").html("Not Selected");
}
});
});
</script>
</head>
<body>
<div>
<form>
<input type="checkbox" name="group" value="1" />1
<input type="checkbox" name="group" value="2" />2
<input type="submit" value="Submit" />
<div id="myText"></div>
</form>
</div>
If no checkboxes have been selected I can create an alert box which informs the user of this, however, ideally, I would like to to avoid using an alert box, and instead, add some feedback text to a DIV I have created.
You can see I am trying to so this with the following code
$("myText").html("Selected");
But for some reason it doesn’t seem to work.
Can anyone please give me some help with this, it would be greatly appreciated.
Thanks.
Tony.
Missing
#inmyTextplease notemytextisidand to access id you need to do this$("#myText").html("Selected");rest should work like rocket.hope this helps the cause
:)