簡體   English   中英

僅選中一項時復選框數組出現Javascript問題

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

我有一個帶有復選框的表單,以這種方式檢查時,這些復選框將值存儲在數組中:

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

在提交時,我會檢查選中了哪些復選框,並對其進行處理。

當僅選擇一項時,listaction不是數組而是一個字符串,就會發生我的問題。

我該如何處理?

一切都從觸發desubmit()的提交按鈕開始

這些是處理功能:

    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; 
        }

您可以檢查它是否為字符串,並以其他方式處理它。 為此使用instanceOf:

if(listaction instanceOf String) ...

我現在完全迷路了...

我添加了此功能以測試數組的類型

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

我在get_args函數中使用此函數放置了一條調試行,如下所示:function get_args(){test(document.ListActionForm ['listaction []']); s = chkboxa2str(document.ListActionForm ['listaction []']); if(s.length <8)返回false; 否則返回s; }

突然間,只有在只選擇了一個元素(我想是字符串)的情況下,數組才被識別,而在選中多個復選框時不再識別

測試線甚至不做任何事情....

這是一個錯誤嗎?

為了使其更簡單,我創建了一個完全正常工作的html頁面,其中的bug表示了自己。

要模擬該問題,只需使用兩個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