问题
I have a form where I have to post form values to my action class. In this form I have a checkbox that needs to be readonly. I tried setting disabled="true" but that doesn't work when posting to the action class.
So please advice??
回答1:
You can easily do this by css. HTML :
<form id="aform" name="aform" method="POST">
<input name="chkBox_1" type="checkbox" checked value="1" readonly />
<br/>
<input name="chkBox_2" type="checkbox" value="1" readonly />
<br/>
<input id="submitBttn" type="button" value="Submit">
</form>
CSS :
input[type="checkbox"][readonly] {
pointer-events: none;
}
Demo
回答2:
There is no property to make the checkbox readonly. But you can try this trick.
<input type="checkbox" onclick="return false" />
DEMO
回答3:
<input type="checkbox" checked onclick="return false;" onkeydown="return false;"/>
http://jsfiddle.net/2srjc/
If you are worried about tab order, only return false for the keydown event when the tab key was not pressed:
<input type="checkbox" checked onclick="return false;" onkeydown="e = e || window.event; if(e.keyCode !== 9) return false;"/>
http://jsfiddle.net/2srjc/149/
回答4:
I personally like to do it this way:
<input type="checkbox" name="option" value="1" disabled="disabled" />
<input type="hidden" name="option" value="1">
I think this is better for two reasons:
- User clearly understand that he can't edit this value
- The value is sent when submitting the form.
回答5:
You may simply add onclick="return false" - this will stop browser executing default action (checkbox checked/not checked will not be changed)
回答6:
Make a fake checkbox with no name and value, force the value in an hidden field:
<input type="checkbox" disabled="disabled" checked="checked">
<input type="hidden" name="name" value="true">
Note: if you put name and value in the checkbox, it will be anyway overwritten by the input with the same name
回答7:
I use JQuery so I can use the readonly attribute on checkboxes.
//This will make all read-only check boxes truly read-only
$('input[type="checkbox"][readonly]').on("click.readonly", function(event){event.preventDefault();}).css("opacity", "0.5");
If you want the checkbox to be editable again then you need to do the following for the specific checkbox.
$('specific checkbox').off('.readonly').removeAttr("readonly").css("opacity", "1")
回答8:
None of the above worked for me. Here's my vanilla.js solution:
(function() {
function handleSubmit(event) {
var form = event.target;
var nodes = form.querySelectorAll("input[disabled]");
for (var node of nodes) {
node.disabled = false;
}
}
function init() {
var submit_form_tag = document.getElementById('new_whatever');
submit_form_tag.addEventListener('submit', handleSubmit, true);
}
window.onload = init_beworst;
})();
Be sure to provide an appropriate replacement for the form id.
My application has a bit of context, where some boxes are pre-checked, and others you have a limit of how many of the other boxes you can check. When you hit that limit, all the non-pre-checked boxes are disabled, and if you uncheck one all the non-pre-checked boxes are enabled again. When the user presses submit all the checked boxes are submitted to the user, regardless of whether they're pre-checked or not.
回答9:
Through CSS:
<label for="">
<input type="checkbox" style="pointer-events: none; tabindex: -1;" checked> Label
</label>
pointer-events not supported in IE<10
https://jsfiddle.net/fl4sh/3r0v8pug/2/
回答10:
document.getElementById("your checkbox id").disabled=true;
来源:https://stackoverflow.com/questions/12267242/how-can-i-make-a-checkbox-readonly-not-disabled