问题
i'm trying to run a function after checkbox checked and pushed submit button with jquery
this is what i have:
<form name="select">
<label for="Genre"><input type="checkbox" checked="checked" id="Genre">Genre</label>
<input type="button" onclick="myFunction()" value="submit">
</form>
this is my function:
function myFunction() {
$("input [id=Genre] [checked]").each(
function() {
alert("genre is checked");
}
);
}
any ideas what i'm doing wrong?
回答1:
If you select by ID you can just use:
$('#Genre')
If you want to select all checkboxes then you can just:
$('input[type="checkbox"]')
If you want to select a checked checkbox you can use:
$('input[type="checkbox"]:checked')
If you want to do something WHEN someone checks a checkbox you need to add an event listener:
$('#Genre').bind('click', function() {
// do something when checkbox with ID Genre is clicked
if ($(this).is(':checked')) {
// do something when the checkbox is checked
}
})
回答2:
$('#formid').submit(function(){
$('#genre:checked').each(function()
{
alert('checked');
})
});
回答3:
Try using : $('input#Genre :checked')
回答4:
The way you attach your function to the submit button is not the "jQuery" way.
Try this :
$('input[value=submit]').on('click', myFunction);
http://jsfiddle.net/5jj8e/
回答5:
$('#formid').submit(function(){
if($('#Genre').is(':checked')){
return true;
}
alert('Not checked');
return false;
});
回答6:
In HTML give the value in radio button
<form name="select">
<label for="Genre"><input type="checkbox" checked="checked" id="Genre" value="Genre1" >Genre</label>
<input type="button" onclick="myFunction()" value="submit">
</form>
And jquery should be
function myFunction() {
if($("input[id='Genre']:checked").length==0)
{
alert("genre is checked");
}
}
回答7:
You can use jQuery's check selector: http://api.jquery.com/checked-selector/
回答8:
$("input#Genre :checked").each(
function() {
alert("genre is checked");
}
);
来源:https://stackoverflow.com/questions/10549205/run-a-function-after-checkbox-checked-and-pushed-submit-button-with-jquery