如果未选中复选框,则如何提交0;如果选中HTML中的复选框,则如何提交1 [英] How to submit 0 if checkbox is unchecked and submit 1 if checkbox is checked in HTML

查看:357
本文介绍了如果未选中复选框,则如何提交0;如果选中HTML中的复选框,则如何提交1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果选中复选框数组中的复选框,如何提交值1;如果未选中,则如何提交值0?我尝试了这个,但是没有运气.我正在尝试在提交表单时在php数组中获取此数组.请帮忙!

How to submit value 1 if a checkbox in a checkbox array is checked, and submit 0 if it's unchecked? I tried this but no luck. I am trying to grab this array in a php array when the form is submitted. Please help!

<input id = 'testName0' type = 'checkbox' name = 'check[0]' value = '1' checked>
<input id='testNameHidden0'  type='hidden' value='0' name='check[0]'>

<input id = 'testName1' type = 'checkbox' name='check[1]' value = '1' unchekced>
<input id='testNameHidden1'  type='hidden' value='0' name='check[1]'>

<input type = 'submit' value = 'Save Changes'>
>
<script>
if(document.getElementById('testName0').checked){
  document.getElementById('testNameHidden0').disabled = true;
}
</script>

<script>
if(document.getElementById('testName1').checked){
  document.getElementById('testNameHidden1').disabled = true;
}
</script>

推荐答案

最简单的一种,不需要javascript ,只需在复选框前放置一个隐藏的输入即可:

Simplest one, no javascript required, just put a hidden input before the checkbox:

<input type="hidden" name="check[0]" value="0" />
<input type="checkbox" name="check[0]" value="1" />

输入名称必须相同.如果选中此复选框,则将提交值1,否则将从隐藏的输入中提交值0.

Inputs need to have the same name. If the checkbox is checked then value 1 will be submitted, otherwise value 0 from the hidden input.

您的案例 javascript解决方案,无需隐藏的输入内容:

Your case javascript solution, no hidden inputs needed:

<script type="text/javascript">
    // when page is ready
    $(document).ready(function() {
         // on form submit
        $("#form").on('submit', function() {
            // to each unchecked checkbox
            $(this + 'input[type=checkbox]:not(:checked)').each(function () {
                // set value 0 and check it
                $(this).attr('checked', true).val(0);
            });
        })
    })
</script>

<form method="post" id="form">
    <input type="checkbox" name="check[0]" value="1" />
    <input type="checkbox" name="check[1]" value="1" />
    <input type="submit" value="Save Changes" />
</form>

PHP解决方案,不需要隐藏的输入:

PHP solution, no hidden inputs needed:

<?php
    // if data is posted, set value to 1, else to 0
    $check_0 = isset($_POST['check'][0]) ? 1 : 0;
    $check_1 = isset($_POST['check'][1]) ? 1 : 0;
?>

<form method="post">
    <input type="checkbox" name="check[0]" value="1" />
    <input type="checkbox" name="check[1]" value="1" />
    <input type="submit" value="Save Changes" />
</form>

编辑:从 jquery 1.6 开始,javascript解决方案不再有效.根据,以下是更合适的解决方案:

EDIT: the javascript solution is not valid anymore as of jquery 1.6. Based on this, a more proper solution is the following:

<script type="text/javascript">
    // when page is ready
    $(document).ready(function() {
         // on form submit
        $("#form").on('submit', function() {
            // to each unchecked checkbox
            $(this).find('input[type=checkbox]:not(:checked)').prop('checked', true).val(0);
        })
    })
</script>

<form method="post" id="form">
    <input type="checkbox" name="check[0]" value="1" />
    <input type="checkbox" name="check[1]" value="1" />
    <input type="submit" value="Save Changes" />
</form>

这篇关于如果未选中复选框,则如何提交0;如果选中HTML中的复选框,则如何提交1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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