如果在表单提交时勾选复选框输入值数据,浏览器是否仅发送复选框输入值数据是标准行为?
如果没有提供值数据,默认值是否总是“on”?
假设上述假设是正确的,那么所有浏览器的这种行为是否一致呢?
如果在表单提交时勾选复选框输入值数据,浏览器是否仅发送复选框输入值数据是标准行为?
如果没有提供值数据,默认值是否总是“on”?
假设上述假设是正确的,那么所有浏览器的这种行为是否一致呢?
当前回答
我用下面的代码解决了这个问题:
HTML表单
<input type="checkbox" id="is-business" name="is-business" value="off" onclick="changeValueCheckbox(this)" >
<label for="is-business">Soy empresa</label>
而javascript函数通过改变复选框值的形式:
//change value of checkbox element
function changeValueCheckbox(element){
if(element.checked){
element.value='on';
}else{
element.value='off';
}
}
服务器检查数据张贴是“打开”还是“关闭”。我使用的是playframework java
final Map<String, String[]> data = request().body().asFormUrlEncoded();
if (data.get("is-business")[0].equals('on')) {
login.setType(new MasterValue(Login.BUSINESS_TYPE));
} else {
login.setType(new MasterValue(Login.USER_TYPE));
}
其他回答
我有一个页面(表单),动态生成复选框,所以这些答案有很大的帮助。我的解决方案与这里的许多解决方案非常相似,但我禁不住认为它更容易实现。
首先,我把一个隐藏的输入框与我的复选框,即。
<td><input class = "chkhide" type="hidden" name="delete_milestone[]" value="off"/><input type="checkbox" name="delete_milestone[]" class="chk_milestone" ></td>
现在,如果所有的复选框都未被选中,那么隐藏字段返回的值将全部关闭。
例如,这里有五个动态插入的复选框,表单post了以下值:
'delete_milestone' =>
array (size=7)
0 => string 'off' (length=3)
1 => string 'off' (length=3)
2 => string 'off' (length=3)
3 => string 'on' (length=2)
4 => string 'off' (length=3)
5 => string 'on' (length=2)
6 => string 'off' (length=3)
这表明只有第3和第4个复选框被选中或选中。
从本质上讲,虚拟或隐藏的输入字段只是表明一切都是关闭的,除非在off索引下面有一个“on”,然后在不需要一行客户端代码的情况下为您提供所需的索引。
.
浏览器只发送复选框输入是标准行为吗 价值数据是否在表单提交时进行检查?
是的,因为否则就没有可靠的方法来确定复选框是否被选中(如果它改变了值,则可能存在这样的情况,即当您选中的期望值与它被交换到的值相同时)。
如果没有提供值数据,默认值是否总是“on”?
其他答案证实“开”是默认值。但是,如果你对值不感兴趣,可以使用:
if (isset($_POST['the_checkbox'])){
// name="the_checkbox" is checked
}
是的,标准行为是仅在选中复选框时才发送值。这通常意味着您需要有一种方法来记住您期望在服务器端出现哪些复选框,因为并非所有数据都是从表单返回的。
默认值总是“on”,这应该在不同浏览器中保持一致。
这在W3C HTML 4推荐中包含:
复选框(和单选按钮)是可以切换的开/关开关 由用户决定。当控制元件被检查时,开关是“打开”的 属性已设置。当提交表单时,只有“on”复选框 控制可以成功。
我用下面的代码解决了这个问题:
HTML表单
<input type="checkbox" id="is-business" name="is-business" value="off" onclick="changeValueCheckbox(this)" >
<label for="is-business">Soy empresa</label>
而javascript函数通过改变复选框值的形式:
//change value of checkbox element
function changeValueCheckbox(element){
if(element.checked){
element.value='on';
}else{
element.value='off';
}
}
服务器检查数据张贴是“打开”还是“关闭”。我使用的是playframework java
final Map<String, String[]> data = request().body().asFormUrlEncoded();
if (data.get("is-business")[0].equals('on')) {
login.setType(new MasterValue(Login.BUSINESS_TYPE));
} else {
login.setType(new MasterValue(Login.USER_TYPE));
}
Just like ASP.NET variant, except put the hidden input with the same name before the actual checkbox (of the same name). Only last values will be sent. This way if a box is checked then its name and value "on" is sent, whereas if it's unchecked then the name of the corresponding hidden input and whatever value you might like to give it will be sent. In the end you will get the $_POST array to read, with all checked and unchecked elements in it, "on" and "false" values, no duplicate keys. Easy to process in PHP.