douli2063 2017-07-25 06:50
浏览 48
已采纳

发送3状态复选框的值

I have developed a 3 state checkbox that allows me to send 3 different values. Here are the values that I have set for a checkbox.

State 0 [] = Uncheck (Value = 0)

State 1 [x] = Checked (Value = 1)

State 2 [-] = Indeterminate (Value = 2)

Now the problem lays when sending the values via POST method. Since Uncheck and Indeterminate are unselected, the values don't get sent and I get a value of 0 on the database on both state 0 or state 2.

This is the logic script for the checkboxes:

<script>

window.onload = function () 
{
    setCheckBoxes(document.getElementById('LEVL1LES1'));
}

function setCheckBoxes(cb) {

    if (cb.value == 0)
    {
        cb.checked = false;
    }
    else if (cb.value == 1)
    {
        cb.checked = true;
    }
    else
    {
        cb.indeterminate=true;
    }
 }

function changeBoxValues(cb) {
    if (cb.value == 0)
    {
        cb.value = 1;
        setCheckBoxes(cb);
    }
    else if (cb.value == 1)
    {
        cb.value = 2;
        setCheckBoxes(cb);
    }
    else
    { 
        cb.value = 0;
        setCheckBoxes(cb);
    }
 }

And HTML:

<input type="checkbox" name="LEVL1LES1" value='<?php echo $row["LEVL1LES1"]?>' id="LEVL1LES1" onclick="changeBoxValues(this)">

Can anyone give me any input on how to fix this problem since I know that I am trying to make a workaround the default behavior oh checkboxes?

PS. You may ask why I don't use other options like dropdown? Here is why:

enter image description here Thanks!

  • 写回答

1条回答 默认 最新

  • douquqiang1513 2017-07-25 07:01
    关注

    If you're doing this in a classic form and always want to receive a value, a fairly standard approach is not to send the checkbox's value at all; instead, have a hidden form field with the value you want to send.

    <input type="checkbox" id="LEVL1LES1" onclick="changeBoxValues(this)">
    <input type="hidden" name="LEVL1LES1" value='<?php echo $row["LEVL1LES1"]?>'>
    

    Notice that the checkbox has no name, and so will not be included in the form when submitted. Also note that the hidden field has a name which matches the id of the checkbox. I use that in changeBoxValues below to relate the two fields, but it's just one way to do it; another would be a data-* attribute or even just cb.nextElementSibling and ensuring that the hidden field is always the next element after the checkbox element.

    Then in changeBoxValues:

    function changeBoxValues(cb) {
        var hidden = document.querySelector('[name="' + cb.id + '"]');
        if (hidden.value == 0)
        {
            hidden.value = 1;
        }
        else if (hidden.value == 1)
        {
            hidden.value = 2;
        }
        else
        { 
            counter = 0; // ?? Where did counter come from??
            hidden.value = counter;
        }
        setCheckBoxes(cb, hidden.value);
    }
    

    ...and setCheckBoxes uses the value passed in rather than cb.value to determine which state to set:

    function setCheckBoxes(cb, value) {
        if (value == 0)
        {
            cb.checked = false;
        }
        else if (value == 1)
        {
            cb.checked = true;
        }
        else
        {
            cb.indeterminate=true;
        }
    }
    

    It's worth noting that both your original approach and the above rely on JavaScript, so you'll need to require that JavaScript be enabled on the page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP