【问题标题】:Javascript issue with checkbox array when only one item is selected仅选择一项时复选框数组的Javascript问题
【发布时间】:2011-01-27 15:37:30
【问题描述】:

我有一个带有复选框的表单,这些复选框在以这种方式检查时将值存储在一个数组中:

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

【问题讨论】:

  • 正在考虑修改函数 chkboxa2str(chkbox_a)

标签: javascript html forms checkbox


【解决方案1】:

您可以检查它是否是字符串并以其他方式处理。为此使用 instanceOf:

if(listaction instanceOf String) ...

【讨论】:

    【解决方案2】:

    我现在完全迷失了......

    我添加了这个函数来测试我的数组的类型

            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 函数中添加了一个带有此函数的调试行,如下所示: 函数 get_args() { 测试(document.ListActionForm['listaction[]']); s=chkboxa2str(document.ListActionForm['listaction[]']); 如果(s.length

    突然间,只有当只有一个元素被选中(我想是字符串)时,数组才会被识别,但当多个复选框被选中时不再被识别

    测试线甚至什么都不做......

    这是一个错误吗?

    【讨论】:

      【解决方案3】:

      为了更简单,我创建了一个完整的 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> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-17
        • 2016-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-12
        相关资源
        最近更新 更多