我有一些代码
<input type="checkbox" id="chk" value="value" />
<label for="chk">Value </label>
<br/>
<input type="button" id="But1" value="set value" />
<br />
<input type="button" id="But2" value="read checked" />
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
$(document).ready(function () {
console.log("Ready ...");
registerHandlers();
function registerHandlers() {
$('#But1').click(function () {
$('#chk').prop('checked', !$('#chk').is(':checked'));
});
$('#But2').click(function () {
var chk1 = $('#chk').is(':checked');
console.log("Value : " + chk1);
});
$('input[type="checkbox"]').change(function () {
var name = $(this).val();
var check = $(this).prop('checked');
console.log("Change: " + name + " to " + check);
});
}
});
Run Code Online (Sandbox Code Playgroud)
如何使用jQuery处理复选框的更改?我需要把处理程序更改为选中的任何复选框.
[更新]
有一个复选框和几个按钮.每个按钮都可以更改复选框.如何捕捉更改复选框的事件?
[更新]
我需要在此示例jsfiddle中使用 handle change复选框.当我单击该框时,不显示消息"确定"按钮.
Sam*_*ich 156
使用:checkbox选择器:
$(':checkbox').change(function() {
// do stuff here. It will fire on any checkbox change
});
Run Code Online (Sandbox Code Playgroud)
Pin*_*rma 62
您也可以使用该字段的ID
$('#checkbox1').change(function() {
if($(this).is(":checked")) {
//'checked' event code
return;
}
//'unchecked' event code
});
Run Code Online (Sandbox Code Playgroud)
Lok*_*dav 16
希望,这会有所帮助.
$('input[type=checkbox]').change(function () {
if ($(this).prop("checked")) {
//do the stuff that you would do when 'checked'
return;
}
//Here do the stuff you want to do when 'unchecked'
});
Run Code Online (Sandbox Code Playgroud)
$("input[type=checkbox]").on("change", function() {
if (this.checked) {
//do your stuff
}
});
Run Code Online (Sandbox Code Playgroud)
$('#myForm').on('change', 'input[type=checkbox]', function() {
this.checked ? this.value = 'apple' : this.value = 'pineapple';
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
275272 次 |
| 最近记录: |