我有一堆默认选中的复选框。我的用户可能会取消选中一些复选框(如果有的话),并选中其余的复选框。
是否有任何方法使表单POST未选中的复选框,而不是选中的复选框?
我有一堆默认选中的复选框。我的用户可能会取消选中一些复选框(如果有的话),并选中其余的复选框。
是否有任何方法使表单POST未选中的复选框,而不是选中的复选框?
当前回答
我更喜欢不需要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>" ;
其他回答
您可以在提交表单之前添加隐藏元素。
$('form').submit(function() {
$(this).find('input[type=checkbox]').each(function (i, el) {
if(!el.checked) {
var hidden_el = $(el).clone();
hidden_el[0].checked = true;
hidden_el[0].value = '0';
hidden_el[0].type = 'hidden'
hidden_el.insertAfter($(el));
}
})
});
我个人最喜欢的是添加一个具有相同名称的隐藏字段,如果复选框未选中,将使用该字段。但解决方案并不像看起来那么容易。
如果你添加以下代码:
<form>
<input type='hidden' value='0' name='selfdestruct'>
<input type='checkbox' value='1' name='selfdestruct'>
</form>
浏览器不会真正关心您在这里做了什么。浏览器将把两个参数都发送给服务器,服务器必须决定如何处理它们。
例如,PHP将最后一个值作为要使用的值(参见:重复HTTP GET查询键的权威位置)
但是我使用过的其他系统(基于Java)是这样做的——它们只提供第一个值。 .NET会给你一个包含这两个元素的数组
我会试着在某个时候用node.js, Python和Perl测试这个。
我先试了山姆的版本。 好主意,但它会导致表单中有多个具有相同名称的元素。如果您使用任何基于名称查找元素的javascript,它现在将返回一个元素数组。
我已经在PHP中解决了Shailesh的想法,它为我工作。 这是我的代码:
/* Delete '.hidden' fields if the original is present, use '.hidden' value if not. */ foreach ($_POST['frmmain'] as $field_name => $value) { // Only look at elements ending with '.hidden' if ( !substr($field_name, -strlen('.hidden')) ) { break; } // get the name without '.hidden' $real_name = substr($key, strlen($field_name) - strlen('.hidden')); // Create a 'fake' original field with the value in '.hidden' if an original does not exist if ( !array_key_exists( $real_name, $POST_copy ) ) { $_POST[$real_name] = $value; } // Delete the '.hidden' element unset($_POST[$field_name]); }
I see this question is old and has so many answers, but I'll give my penny anyway. My vote is for the javascript solution on the form's 'submit' event, as some has pointed out. No doubling the inputs (especially if you have long names and attributes with php code mixed with html), no server side bother (that would require to know all field names and to check them down one by one), just fetch all the unchecked items, assign them a 0 value (or whatever you need to indicate a 'not checked' status) and then change their attribute 'checked' to true
$('form').submit(function(e){
var b = $("input:checkbox:not(:checked)");
$(b).each(function () {
$(this).val(0); //Set whatever value you need for 'not checked'
$(this).attr("checked", true);
});
return true;
});
这样你会得到一个像这样的$_POST数组:
Array
(
[field1] => 1
[field2] => 0
)
所有答案都很好,但如果您在一个表单中有多个具有相同名称的复选框,并且您希望发布每个复选框的状态。然后我已经解决了这个问题,通过放置一个隐藏字段的复选框(名称相关的我想要的)。
<input type="hidden" class="checkbox_handler" name="is_admin[]" value="0" />
<input type="checkbox" name="is_admin_ck[]" value="1" />
然后用jquery代码控制复选框的变化状态:
$(documen).on("change", "input[type='checkbox']", function() {
var checkbox_val = ( this.checked ) ? 1 : 0;
$(this).siblings('input.checkbox_handler').val(checkbox_val);
});
现在改变任何复选框,它将改变相关隐藏字段的值。在服务器上,你只能看到隐藏字段,而不是复选框。
希望这能帮助到有这种问题的人。快乐:)