简体   繁体   English

仅选中一项时复选框数组出现Javascript问题

[英]Javascript issue with checkbox array when only one item is selected

I have a form with checkboxes, these checkboxes store there value in an array when checked this way: 我有一个带有复选框的表单,以这种方式检查时,这些复选框将值存储在数组中:

<input type='checkbox' name='listaction[]' value='2010102909103530'>

On submit I check which checkboxes were checked and I do something with it. 在提交时,我会检查选中了哪些复选框,并对其进行处理。

My problem occurs when only one item is selected then listaction isn't an array but just a string ... 当仅选择一项时,listaction不是数组而是一个字符串,就会发生我的问题。

How do I handle this ? 我该如何处理?

It starts all with the submit button that firesup desubmit() 一切都从触发desubmit()的提交按钮开始

These are the handling functions: 这些是处理功能:

    function desubmit()
        {
        if(get_args()==false) {alert("U hebt geen treinen geselecteerd!");return false;}
        if(labelling(true)) return false;
        }
        function Check(chk)
        {
        for (i=0; i < chk.length; i++) chk[i].checked=document.ListActionForm.Check_ctr.checked ;
        }
        function labelling(s)
        {
        notrains="U hebt geen treinen geselecteerd!"
        selectval=document.ListActionForm.la.options[document.ListActionForm.la.selectedIndex].value;
        if(selectval=='exportoptions') {popUpWin('form.php?exportconfig=1','console3',470,470);}
        else if(selectval=='newlabel'&&!s) {jPrompt('Nieuwe Lijst:','Default', 'Maak nieuwe lijst (Max 20 karakters)', function(r) {if(r) {if(r.length>20){alert("Gekozen naam lijst mag maximum 20 tekens lang zijn (Overige tekens worden automatisch verwijderd)");r=r.substr(0,20);};document.ListActionForm.newlabel.value=r;document.getElementById('shownewlabel').innerHTML='[ Nieuwe Lijst: '+r+' ]';}});document.getElementById('popup_prompt').maxlength=5;}
        else if(selectval=='export:pdf') {if(arg=get_args()) get_page('/PDF/pdf.php','ids',arg);else alert(notrains);}
        else if(selectval=='export:csv') {if(arg=get_args())get_page('?export=csv','ids',arg);else alert(notrains);}
        else if(selectval=='export:xlsapp') {if(arg=get_args())get_page('?export=excelvbs','ids',arg);else alert(notrains);}
        else if(selectval=='export:xlsapptxt') {if(arg=get_args())get_page('?export=excelvbstxt','ids',arg);else alert(notrains);}
        else return false;
        return true;
        }
    function get_args()
        {
        s=chkboxa2str(document.ListActionForm['listaction[]']);
        if(s.length<8)return false;
        else return s;
        }
        function chkboxa2str(chkbox_a) { 
        var list = ""; 
        for(var i = 0; i < chkbox_a.length; i++){ if(chkbox_a[i].checked) { list += chkbox_a[i].value + " "; } } 
        return list; 
        }

You can check if it's string and handle it in other way. 您可以检查它是否为字符串,并以其他方式处理它。 Use instanceOf for this: 为此使用instanceOf:

if(listaction instanceOf String) ...

I'm now completely lost ... 我现在完全迷路了...

I added this function to test the type off my array 我添加了此功能以测试数组的类型

        function test (obj) {
        var type = typeof obj;
        if (type == 'object') {
        if (obj.getDate) return 'Date';
        if (obj.split) return 'String';
        return object;
        }
        return type;
        }

and I putted a debug line with this function in get_args function like this: function get_args() { test (document.ListActionForm['listaction[]']); 我在get_args函数中使用此函数放置了一条调试行,如下所示:function get_args(){test(document.ListActionForm ['listaction []']); s=chkboxa2str(document.ListActionForm['listaction[]']); s = chkboxa2str(document.ListActionForm ['listaction []']); if(s.length<8)return false; if(s.length <8)返回false; else return s; 否则返回s; } }

And suddenly the array is only recognised when there is only one element selected (I suppose as string) but not anymore when multiple checkboxes are selected 突然间,只有在只选择了一个元素(我想是字符串)的情况下,数组才被识别,而在选中多个复选框时不再识别

The test line doesnt even do anything .... 测试线甚至不做任何事情....

Is this a bug ? 这是一个错误吗?

To make it much more simple I've createad a fully working html page where the bug represents itself. 为了使其更简单,我创建了一个完全正常工作的html页面,其中的bug表示了自己。

To simulate the problem just run it once with two html input checkbox elements -> check one checkbox and press submit -> it works -> Now remove one input checkbox field and check the remaining checkbox & submit -> Bamm doesn't work... 要模拟该问题,只需使用两个html输入复选框元素运行一次->选中一个复选框,然后按提交->它可以正常工作->现在删除一个输入复选框字段,然后检查其余复选框并提交-> Bamm不起作用。 。

<html>
<head>
            <script>
            function Check(chk)
            {
            for (i=0; i < chk.length; i++) chk[i].checked=document.ListActionForm.Check_ctr.checked ;
            }
        function desubmit()
            {
            if(get_args()==false) {alert("$t_notrains_selected");return false;}
            if(labelling(true)) return false;
            }
            function labelling(s) {return true;}
        function get_args()
            {
            s=chkboxa2str(document.ListActionForm['listaction[]']);
            if(s)alert(s);
            if(s.length<8)return false;
            else return s;
            }
            function chkboxa2str(chkbox_a) { 
            var list = ""; 
                for(var i = 0; i < chkbox_a.length; i++){ if(chkbox_a[i].checked) { list += chkbox_a[i].value + " "; } }
            return list; 
            }
            </script>
</head>
<body>
<form action="?h=1296078874" method="post" name="ListActionForm" onsubmit="return desubmit()">
<input type='checkbox' name='listaction[]' value='2010102909103530'> Testbox 1<br>
<input type='checkbox' name='listaction[]' value='2010102909103532'> Testbox 2<br>
<input type="Submit" name="Submit" value="Versturen" >
</form>
</body>
</html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM