How to ignore clicks for checkboxes?

我与影子孤独终老i 提交于 2019-12-21 20:25:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!