我有一堆默认选中的复选框。我的用户可能会取消选中一些复选框(如果有的话),并选中其余的复选框。
是否有任何方法使表单POST未选中的复选框,而不是选中的复选框?
我有一堆默认选中的复选框。我的用户可能会取消选中一些复选框(如果有的话),并选中其余的复选框。
是否有任何方法使表单POST未选中的复选框,而不是选中的复选框?
当前回答
这里的大多数答案都需要使用JavaScript或重复的输入控件。有时这需要完全在服务器端处理。
我相信解决这个常见问题的关键是表单的提交输入控件。
要成功地解释和处理复选框的未选中值,您需要具备以下知识:
复选框的名称 表单提交输入元素的名称
通过检查表单是否已提交(将一个值分配给提交输入元素),可以假定任何未选中的复选框值。
例如:
<形式 <输入=“value1”类型=“checkbox”值=“1”> checkbox One<br/> <输入=“value2”类型=“checkbox”=“价值”1“checked=“checked=”>“checked”> 2 <br/> <输入=“value3”类型=“checkbox”值=“1”> checkbox Three<br/> <输入=“submit”类型=“submit”值=“submit”> < / form >
在使用PHP时,检测选中了哪些复选框是相当简单的。
<?php
$checkboxNames = array('value1', 'value2', 'value3');
// Persisted (previous) checkbox state may be loaded
// from storage, such as the user's session or a database.
$checkboxesThatAreChecked = array();
// Only process if the form was actually submitted.
// This provides an opportunity to update the user's
// session data, or to persist the new state of the data.
if (!empty($_POST['submit'])) {
foreach ($checkboxNames as $checkboxName) {
if (!empty($_POST[$checkboxName])) {
$checkboxesThatAreChecked[] = $checkboxName;
}
}
// The new state of the checkboxes can be persisted
// in session or database by inspecting the values
// in $checkboxesThatAreChecked.
print_r($checkboxesThatAreChecked);
}
?>
初始数据可以在每个页面加载时加载,但只有在提交表单时才应该修改。由于预先知道复选框的名称,因此可以逐个遍历和检查它们,因此它们的单独值的缺失表明它们没有被选中。
其他回答
$('form').submit(function () {
$(this).find('input[type="checkbox"]').each( function () {
var checkbox = $(this);
if( checkbox.is(':checked')) {
checkbox.attr('value','1');
} else {
checkbox.after().append(checkbox.clone().attr({type:'hidden', value:0}));
checkbox.prop('disabled', true);
}
})
});
我们可以检查$_POST/Request元素是否有它。然后将它赋值给一个变量。
$value = (null !== $_POST['checkboxname']) ?“是”:“不是”;
这与isset()函数非常相似。但是isset()函数不能用于表达式的结果。我们可以用null检查它的结果,并达到相同的效果。
最后的值只是虚数。如果所需字段为布尔类型,我们可以将“YES”更改为1,将“NO”更改为0。
我的做法有点不同。首先,我更改了所有未选中复选框的值。到“0”,然后全部选中,因此该值将被提交。
函数checkboxvalues () { $ (" # checkbox-container输入:复选框"). each函数({ if ($ () .prop(“检查”)! = true) { (美元).val(“0”); (美元)。道具(“检查”,真正的); } }); }
我使用这个jQuery块,它将在提交时为每个未选中的复选框添加一个隐藏输入。它将保证您每次都为每个复选框提交一个值,而不会弄乱您的标记,并冒着忘记在稍后添加的复选框上执行此操作的风险。它也与您正在使用的任何后端堆栈(PHP、Ruby等)无关。
// Add an event listener on #form's submit action...
$("#form").submit(
function() {
// For each unchecked checkbox on the form...
$(this).find($("input:checkbox:not(:checked)")).each(
// Create a hidden field with the same name as the checkbox and a value of 0
// You could just as easily use "off", "false", or whatever you want to get
// when the checkbox is empty.
function(index) {
var input = $('<input />');
input.attr('type', 'hidden');
input.attr('name', $(this).attr("name")); // Same name as the checkbox
input.attr('value', "0"); // or 'off', 'false', 'no', whatever
// append it to the form the checkbox is in just as it's being submitted
var form = $(this)[0].form;
$(form).append(input);
} // end function inside each()
); // end each() argument list
return true; // Don't abort the form submit
} // end function inside submit()
); // end submit() argument list
“我有一大堆默认选中的复选框”——这就是我解决问题的方法:
if(($C1)OR($C2)OR... ($C18)){echo "some are checked!";} else{$C1='set';$C2='set';$C3='set';$C4='set';$C5='set';$C6='set';$C7='set';$C8='set';$C9='set';$C10='set';$C11='set';$C12='set';$C13='set';$C14='set';$C15='set';$C16='set';$C17='set';$C18='set';} //(if all are unchecked - set them to 'check' since its your default) the above line will execute the echo if some are unchecked but the checked ones will still have the value parameter set therefore, to keep them set, when writing them in the form, use if($C1){echo "checked";} use the values in the further logic...
! !这种方法的局限性:你不能取消选中所有的东西——它们都会被重新选中