donglv9116 2013-01-23 19:21
浏览 52
已采纳

我应该何时使用不是二进制(1或0)值的复选框值? [关闭]

I see lots of people on stackoverflow, across the internet, and my colleagues use values in their checkboxes that are not 1 or 0 (ex. <input type='checkbox' value='check'/>), but my thought is that essentially a checkbox will always be a yes/no answer.

Currently I use a jQuery script I wrote to standardize across my applications that looks like:

;(function($){
    $(document).ready(function() {
        var selector = 'input[type="checkbox"]';
        var c = 'checked';
        $.each($(selector),function(){
            var $el,value,attr;
            $el = $(this);
            value = $el.val();
            attr = $el.attr(c);
            if(value == 1 || typeof(attr) !== 'undefined')
            {
                $el.attr(c,c);
                $el.prop(c,true);
                $el.val(1);
            }
            else{
                $el.removeAttr(c);
                $el.prop(c,false);
                $el.val(0);
            }
        });
        $(selector).change(function(e){
            var $el, checked = null;
            $el = $(this);
            checked = $el.prop(c);
            if (checked) {
                $el.attr(c,c);
                $el.val(1);
            } else {
                $el.removeAttr(c);
                $el.val(0);
            }
        });
    });
})(jQuery);

So that I can handle any of these situations:

<input type='checkbox' value='1'/>
<input type='checkbox' checked/>
<input type='checkbox' checked='checked'/>
<input type='checkbox' value='1' disabled/>
<input type='checkbox' checked disabled/>
<input type='checkbox' checked='checked' disabled/>
<input type='checkbox' value='0'/>
<input type='checkbox'/>
<input type='checkbox' value='0' disabled/>
<input type='checkbox' disabled/>

But it does not account for any values of the checkboxes that are not 0 or 1 or having the attribute 'checked'.

My question is: Is there ever a good use-case for using a non 0/null or 1 value (especially when working with a relational database)?

  • 写回答

1条回答 默认 最新

  • dongtaigan1594 2013-01-23 19:28
    关注

    Consider the following: "chose your favorite fruits (check all that apply)".

    your method:

    <input type="checkbox" name="choice_apple" value="1" />
    <input type="checkbox" name="choice_orange" value="1" />
    <input type="checkbox" name="choice_durian" value="1" />
    

    or you could use something like:

    <input type="checkbox" name="choice[]" value="apple" />
    <input type="checkbox" name="choice[]" value="orange" />
    <input type="checkbox" name="choice[]" value="durian" />
    

    the back-end storage system is irrelevant to this. we're just talking about checkboxes. your method requires 3 separate form field names, with the "value" of the choice embedded in the name. Extra overhead is required to extract that name so you can actually store the fruit's name somewhere.

    Whereas, with the "useful value" version, it's one single form field. PHP will convert the choices to an array - and you end up with the exact name you need, without extra any extraction/processing required.

    Remember: checkboxes which aren't selected are NOT submitted with a normal form submission. There's an implicit true/false right there already. If you get a checkbox's field name at the server, the checkbox WAS selected.

    Since you already have a nicely built-in true/false, why not layer on the extra semantics of saying what the true/false represents?

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题