取消选中时,复选框返回false [英] Make checkbox return false when unchecked

查看:316
本文介绍了取消选中时,复选框返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的表单:

 < form> 
< input typecheckboxname =checked [(unique_id)]>
< input typecheckboxname =checked [(unique_id)]>
< input typecheckboxname =checked [(unique_id)]>
< input typecheckboxname =checked [(unique_id)]>
< / form>

复选框的数量将随时间变化,所以当使用PHP处理这个数据时,我必须循环 _POST ['checked'] 数组。



我的问题是,被检查,当它不是。但只有选中的复选框将添加到 _POST ['checked'] 数组。

解决方案

 < form> 
< input type =checkboxkey =1/>
< input type =hiddenname =checked [1]value =false>
< input type =checkboxkey =2/>
< input type =hiddenname =checked [2]value =false>
< input type =checkboxkey =3/>
< input type =hiddenname =checked [3]value =false>
< input type =checkboxkey =4/>
< input type =hiddenname =checked [4]value =false>
< / form>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< / script>
< script>
$(document).ready(function(){
$('[key]')change(function(){
var key = $(this).attr '');
$($('[name =checked ['+ key]'''''))val($(this).is(':checked')?'true' false');
});
});
< / script>

这里是我在做什么



我使用两个输入一个是没有名称的复选框,所以它不会被发送到php
,另一个是隐藏的不会显示给用户,但它是将被发送到php



然后使用jquery当用户选中框jquery将隐藏输入的值更改为true,取消选中时将其更改为false



,所以该值将始终以 string



的形式发送给值为true或false的php。您可以通过更改此
.is(':checked')?'true':'false')更改要发送到php的值
到类似 .is(':checked')?1:0)发送1和0而不是true和false



另一种解决方案是rybo111解决方案

 < input type =hiddenname =checkvalue =false> 
< input type =checkboxname =checkvalue =true>

它会发送两个选项,但如果复选框被选中,它将覆盖第一个选项



但它不可靠100%,它会向服务器发送更多数据



阅读更多关于发布具有相同名称属性的表单字段



所以如果你想使用简单的解决方案没有js使用html only
如果你想100%可靠的解决方案使用js


I have a form looking like:

<form>
    <input type"checkbox" name="checked[(unique_id)]">
    <input type"checkbox" name="checked[(unique_id)]">
    <input type"checkbox" name="checked[(unique_id)]">
    <input type"checkbox" name="checked[(unique_id)]">
</form>

The number of checkboxes will variate from time to time so when processing this data with PHP I have to loop the _POST['checked'] array.

My problem is that I want to take actions both when a checkbox is checked and when it's not. But only the the checked checkboxes will be added to the _POST['checked'] array.

解决方案

<form>
    <input type="checkbox" key="1"/>
    <input type="hidden" name="checked[1]" value="false">
    <input type="checkbox" key="2"/>
    <input type="hidden" name="checked[2]" value="false">
    <input type="checkbox" key="3"/>
    <input type="hidden" name="checked[3]" value="false">
    <input type="checkbox" key="4"/>
    <input type="hidden" name="checked[4]" value="false">
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $('[key]').change(function () {
            var key = $(this).attr('key');
            $($('[name="checked[' + key + ']"]')).val($(this).is(':checked') ? 'true' : 'false');
        });
    });
</script>

here is what i'm doing

i'm using two inputs one is checkbox without name so it won't be sent to php , the other is hidden won't be shown to the user but it is what will be sent to php

then with jquery when the user check the box jquery change the value of the hidden input to true and when uncheck it change the value to false

so the value will always be send to the php with value true or false as string

you can change the value you want to send to php by changing this .is(':checked')?'true':'false') to something like that .is(':checked')?1:0) to send 1 and 0 instead of true and false

another solution is rybo111 solution

<input type="hidden" name="check" value="false">
<input type="checkbox" name="check" value="true">

it will send the two options but if the checkbox is checked it will override the first option

but it is not reliable 100% and it will send more data to the server

read more about that in POSTing Form Fields with same Name Attribute

so if you want to use simple solution without js use the "html only" if you want 100% reliable solution use the "js"

这篇关于取消选中时,复选框返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆