问题
I need to attach a function to a checkbox so that clicking it does nothing. How is this possible? I don't want it to be greyed out, I just want to stop it from being togglable.
回答1:
If you want it to be toggleable, just attach an event listener as follows:
$('#checkboxId').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
});
Note: This is the same effect as returning false
:
$('#checkboxId').on('click', function(e) {
return false;
});
回答2:
Try something like this:
<input type="checkbox" onClick="this.checked=!this.checked" />
回答3:
$('input[type=checkbox]').click(function(){return false;});
just return false on click event, working sample: http://jsfiddle.net/Sh8Zu/
回答4:
try this,
<input type="checkbox" onChange="return checkChange(this)" id="checkid"/>
function checkChange(obj)
{
obj.checked=false;
}
回答5:
I am not sure if this is exactly what you want but this disables the checkbox using JQuery.
$('#checkboxId').attr('disabled', true);
For a non-grey out solution:
$("input:checkbox").click(function() { return false; });
来源:https://stackoverflow.com/questions/15578792/how-to-ignore-clicks-for-checkboxes