我有一堆默认选中的复选框。我的用户可能会取消选中一些复选框(如果有的话),并选中其余的复选框。
是否有任何方法使表单POST未选中的复选框,而不是选中的复选框?
我有一堆默认选中的复选框。我的用户可能会取消选中一些复选框(如果有的话),并选中其余的复选框。
是否有任何方法使表单POST未选中的复选框,而不是选中的复选框?
当前回答
有一个更好的解决办法。 首先,只向选中的复选框提供name属性。 然后点击提交,通过JavaScript函数检查所有未选中的框。 当你提交时,只有那些会在表单集合中检索到那些有name属性的。
//removing name attribute from all checked checkboxes
var a = $('input:checkbox:checked');
$(a).each(function () {
$(this).removeAttr("name");
});
// checking all unchecked checkboxes
var b = $("input:checkbox:not(:checked)");
$(b).each(function () {
$(this).attr("checked", true);
});
优势: 不需要创建额外的隐藏框,并操作它们。
其他回答
有一个更好的解决办法。 首先,只向选中的复选框提供name属性。 然后点击提交,通过JavaScript函数检查所有未选中的框。 当你提交时,只有那些会在表单集合中检索到那些有name属性的。
//removing name attribute from all checked checkboxes
var a = $('input:checkbox:checked');
$(a).each(function () {
$(this).removeAttr("name");
});
// checking all unchecked checkboxes
var b = $("input:checkbox:not(:checked)");
$(b).each(function () {
$(this).attr("checked", true);
});
优势: 不需要创建额外的隐藏框,并操作它们。
我更喜欢不需要javascript的解决方案(是的,我知道现在会有一些人把我归为勒德分子),但如果你像我一样,你想要能够显示复选框的默认/初始设置,然后让它保持或更新基于提交的表单,试试这个(建立在上面的几个想法上):
设置确定初始复选框状态的变量,然后使用服务器检查确定每次提交表单后的显示。
下面的例子创建了一个数组的数组(用于WordPress;同样适用于其他地方),然后使用服务器脚本确定复选框的状态,而不使用隐藏字段或javascript。
<?php
$options_array = array (
array('list_companies_shortcode' , 'shortcode_list_companies' , 'List Companies Shortcode'),
array('list_contacts_shortcode' , 'shortcode_list_contacts' , 'List Contacts Shortcode'),
array('test_checkbox_1' , 'checked' , 'Label for Checkbox 1' , 'checkbox' , 'Some text to instruct the user on this checkbox use' ),
array('test_checkbox_2' , '' , 'Label for Checkbox 2' , 'checkbox' , 'Some other text to instruct the user on this checkbox use' )
) ;
$name_value_array = array() ;
$name_label_array = array() ;
$field_type_array = array() ;
$field_descriptor_array = array() ;
foreach ( $options_array as $option_entry ) {
$name_value_array[ $option_entry[0] ] = $option_entry[1] ;
$name_label_array[ $option_entry[0] ] = $option_entry[2] ;
if ( isset( $option_entry[3] ) ) {
$field_type_array[ $option_entry[0] ] = $option_entry[3] ;
$field_descriptor_array[ $option_entry[0] ] = $option_entry[4] ;
} else {
$field_type_array[ option_entry[0] ] = 'text' ;
$field_descriptor_array[ $option_entry[0] ] = NULL ;
}
}
echo "<form action='' method='POST'>" ;
foreach ( $name_value_array as $setting_name => $setting_value ) {
if ( isset( $_POST[ 'submit' ] ) ) {
if ( isset( $_POST[ $setting_name ] ) ) {
$setting_value = $_POST[ $setting_name ] ;
} else {
$setting_value = '' ;
}
}
$setting_label = $option_name_label_array[ $setting_name ] ;
$setting_field_type = $field_type_array[ $setting_name ] ;
$setting_field_descriptor = $field_descriptor_array [ $setting_name ] ;
echo "<label for=$setting_name >$setting_label</label>" ;
switch ( $setting_field_type ) {
case 'checkbox':
echo "<input type='checkbox' id=" . $setting_name . " name=" . $setting_name . " value='checked' " . $setting_value . " >(" . $setting_field_descriptor . ")</input><br />" ;
break ;
default:
echo "<input type='text' id=" . $setting_name . " name=" . $setting_name . " value=" . $setting_value . " ></input><br />" ;
}
}
echo "<input type='submit' value='Submit' name='submit' >" ;
echo "</form>" ;
“我有一大堆默认选中的复选框”——这就是我解决问题的方法:
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...
! !这种方法的局限性:你不能取消选中所有的东西——它们都会被重新选中
当提交时复选框未选中时,将复选框的值更新为'NO'并设置checked = 'TRUE'
https://jsfiddle.net/pommyk/8d9jLrvo/26/
$(document).ready(function()
{
function save()
{
if (document.getElementById('AgeVerification').checked == false)
{
document.getElementById('AgeVerification').value = 'no';
document.getElementById('AgeVerification').checked = true;
}
}
document.getElementById("submit").onclick = save;
})
这里的大多数答案都需要使用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);
}
?>
初始数据可以在每个页面加载时加载,但只有在提交表单时才应该修改。由于预先知道复选框的名称,因此可以逐个遍历和检查它们,因此它们的单独值的缺失表明它们没有被选中。