【问题标题】:Show warning on form submit if no checkbox is check JavaScript如果没有复选框选中 JavaScript,则在表单提交时显示警告
【发布时间】:2014-05-22 12:19:25
【问题描述】:

我已经在一个表单上放置了一个 onsubmit 函数,它会检查某些复选框是否被选中。

我遇到的问题是我只需要检查每列中的一个复选框,总共有 3 列,每列中大约有 6 个不同的复选框 - 列中的每个复选框共享相同的 ID(这就是让我很难)。

下面的脚本有效,但它需要检查每列中的所有框,我如何修改它以仅在每列中的一个复选框被选中时才提交。

<script type="text/javascript">
window.onload = function() {
    var form = document.getElementById('uwpqsffrom_731');
    form.onsubmit = function() {
        var no1 = 0, no2 = 0, no3 = 0;
    var list;
    list = document.getElementsByName('taxo[0][term][]');
    for(i=0; i<list.length; i++)
    {
       if (list[i].checked)
       {
           no1++;
       }
    }
    list = document.getElementsByName('taxo[1][term][]');
    for(i=0; i<list.length; i++)
    {
       if (list[i].checked)
       {
           no2++;
       }
    }
    list = document.getElementsByName('taxo[2][call]');
    for(i=0; i<list.length; i++)
    {
       if (list[i].checked)
       {
           no3++;
       }
    }
    list = document.getElementsByName('taxo[2][term][]');
    for(i=0; i<list.length; i++)
    {
       if (list[i].checked)
       {
           no3++;
       }
    }
    if ( no1 == 0 || no2 == 0 || no3 == 0 )
    {
        alert("Please select at least one option from each section");
        return false;
    }
    }
}
</script>

HTML:

您可以看到有多个具有相同 ID 的复选框 - 我只需要检查其中的 1 个即可提交表单。

<form id="uwpqsffrom_731" method="get" action="http://test.com/">

<div class="uform_title">Find Your Perfect Venue</div><input type="hidden" name="unonce" value="a92b348757"><input type="hidden" name="uformid" value="731"><input type="hidden" name="s" value="uwpsfsearchtrg">

<div class="uwpqsf_class  tax-check-0 togglecheck">
<span class="taxolabel-0">Guests</span>
<input type="hidden" name="taxo[0][name]" value="number-of-guests">
<input type="hidden" name="taxo[0][opt]" value="1">
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="150-180">150-180</label>
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="180-200">180-200</label>
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="20-50">20-50</label>
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="200-350">200-350</label>
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="200-380">200-380</label>
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="350-500">350-500</label>
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="50-65">50-65</label>
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="500-1000">500-1000</label>
<label><input type="checkbox" id="tchkb-0" name="taxo[0][term][]" value="65-150">65-150</label>
</div>

<div class="uwpqsf_class  tax-check-1 togglecheck">
<span class="taxolabel-1">Event Type</span>
<input type="hidden" name="taxo[1][name]" value="event-type">
<input type="hidden" name="taxo[1][opt]" value="1">
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="awards-dinner">Awards Dinner</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="awards-dinner-dance">Awards Dinner Dance</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="barbat-mitzvah">Bar/Bat Mitzvah</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="cocktail-party">Cocktail Party</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="dinner">Dinner</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="dinner-dance">Dinner Dance</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="networking-event">Networking Event</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="party">Party</label>
<label><input type="checkbox" id="tchkb-1" name="taxo[1][term][]" value="vip-experience">VIP Experience</label>
</div>

<div class="uwpqsf_class  tax-check-2 togglecheck">
<span class="taxolabel-2">Venue Preference</span>
<input type="hidden" name="taxo[2][name]" value="venue-locations">
<input type="hidden" name="taxo[2][opt]" value="">
<label><input type="checkbox" id="tchkb-2" name="taxo[2][call]" class="chktaxoall">All Venues</label>
<label><input type="checkbox" id="tchkb-2" name="taxo[2][term][]" value="london-dungeon">London</label>
<label><input type="checkbox" id="tchkb-2" name="taxo[2][term][]" value="madame-tussauds">New York</label>
<label><input type="checkbox" id="tchkb-2" name="taxo[2][term][]" value="sea-life">Russia</label>
<label><input type="checkbox" id="tchkb-2" name="taxo[2][term][]" value="stardome-4d">Spain</label>
</div>

<div class="uwpqsf_class  uwpqsf_submit">
<input type="submit" id="uwpqsf_id_btn" value="Search" alt="[Submit]" class="usfbtn ">
</div>
</form>

【问题讨论】:

  • 如果您也可以添加 HTML 将非常有帮助
  • 好的,我添加了一些示例 HTML,这是一个巨大的表单,所以我无法全部粘贴!
  • 为什么要分配相同的 id?
  • 我看不出它们是如何分成列的,所以我只是猜测。首先,您不能对超过 1 个复选框使用相同的 id。如果您想通过同一个选择器选择多个项目,请使用该类。此外,如果您没有将 3 列彼此分开(在不同的容器中或具有类或属性),您需要单独检查每一列。所以我建议如果这是你的实际 HTML 来改变它。
  • 每一列都包含在一个单独的容器 (div) 中 HTML 代码只是复选框的一个示例 - 我了解他关于在多个复选框上使用相同 ID 的问题,但恐怕这是不在我的手中,因为它是一个生成表单的插件。

标签: javascript


【解决方案1】:

对您原来的 javascript 进行了一些更改,但您应该能够轻松地将我的代码复制粘贴到您的代码上。

function test() {
var no1 = 0, no2 = 0, no3 = 0;
var list;
list = document.getElementsByName('taxo[0][term][]');
for(i=0; i<list.length; i++)
{
   if (list[i].checked)
   {
       no1++;
   }
}
list = document.getElementsByName('taxo[1][term][]');
for(i=0; i<list.length; i++)
{
   if (list[i].checked)
   {
       no2++;
   }
}
list = document.getElementsByName('taxo[2][call]');
for(i=0; i<list.length; i++)
{
   if (list[i].checked)
   {
       no3++;
   }
}
list = document.getElementsByName('taxo[2][term][]');
for(i=0; i<list.length; i++)
{
   if (list[i].checked)
   {
       no3++;
   }
}

if ( no1 == 0 || no2 == 0 || no3 == 0 )
{
    alert("Please select at least one option from each section");
    return false;
}

}

对不起,这有点草率,没有jQuery,我真的不能很好地处理javascript,并且由于您的标记而存在一些限制(根本没有改变它,正如您所说的那样它都是由插入。

不管怎样,这里也有一个jsfiddle

(注意:必须为最后一列输入两次for,因为我们的名称不同......)

(注2:如果你也可以使用jQuery,我可以很好地清理它)

如果您只需要从每列中选择一个checkboxes,我建议您使用radio-buttons。如果不能,就改成if ( no1 !=1 &amp;&amp; no2 != 1 &amp;&amp; no3 != 1 )

【讨论】:

  • 你我的朋友真棒 - 它在 jsfiddle 中完美运行,但是当我将它放在网站上时没有任何反应,控制台中也没有任何相关内容:(
  • 您是否将我的代码添加到您拥有的绑定函数中,或者您是否将其作为新函数保留为 onsubmit 调用它?
  • 我想我知道为什么了,我真的有点蠢——我需要将 onSubmit 函数放到表单中。我应该只从脚本中取出第一部分并将其标记在前面吗?
  • 在这两种情况下都应该工作是的。要么将函数作为 onsubmit 添加到表单中,要么将我的代码(不带函数)粘贴到代码的开头
  • 好的,我已经用 JavaScript 代码更新了我的问题,使用您提供的代码 + 将其包装在我的“绑定函数”中,它不起作用 - 看起来还可以吗?
【解决方案2】:

如何修改它以仅在每列中的一个复选框被选中时仍提交。

您应该使您的所有checkbox 具有通用名称。然后用名字迭代checkbox,使用document.getElementsByName('name')

您的html 应该如下所示:

150-180 180-200

var chkBox = document.getElementsByName('taxo');

for (var i = 0; i < chkBox.length; i++){
     if (chkBox.checked == true){

         submitForm();

         break;
     }
}

function submitForm(){
    //submit form
}

【讨论】:

  • 感谢您的回答,问题是我需要从每列中选中一个框(总共 3 个) - 如果总共选中一个框,这不会仍然发送表单吗?
猜你喜欢
  • 2012-11-02
  • 1970-01-01
  • 2015-07-17
  • 2016-03-14
  • 1970-01-01
  • 2023-02-17
  • 1970-01-01
  • 1970-01-01
  • 2016-02-13
相关资源
最近更新 更多