【问题标题】:multiple grouped radio button validation javascript [closed]多个分组单选按钮验证javascript [关闭]
【发布时间】:2014-06-03 10:12:56
【问题描述】:

我有多个名为 optionRadios1 到 5 的分组单选按钮。我想通过 javascript 验证它们,但我的验证代码不起作用,但仍继续提交表单。

这是我的代码:

function submitForm(){

var counter = "<?php echo $count;?>"; //$count is the total number of grouped radio buttons

for(j=1;j<=counter;j++) {   
        var radioname = "optionRadios" + j;

        var elem=document.forms['test-form'].elements[radioname];
        len=elem.length-1;
        chkvalue='';

        for(i=0; i<=len; i++){
            if(elem[i].checked){chkvalue=elem[i].value;}
        }
        if(chkvalue=='') {
            alert('There are still unanswered questions. ' + radioname);
        return false;

        }else {
         $('#test-form').submit();

        }
    }
}

这是应该使用的形式:

<form action="#" method="post" id="test-form">
//lots of codes here

<button type="submit" class="btn btn-primary" onclick="return submitForm();" id="next">Next</button>


</form>

【问题讨论】:

  • 想要澄清“我的代码不起作用”?
  • 抱歉,我的 javascript 验证码不起作用
  • nmean not working 是什么意思?
  • 好的,怎么它不起作用?它不应该做什么,它做什么它应该?您可以在JS Fiddle 或类似网站上设置一个现场演示并说明缺陷吗?
  • still continues to submit the form 那你怎么防止呢???控制台中的任何错误?在你这边调试呢?!

标签: javascript php jquery


【解决方案1】:

试试这个

$('#test-form').submit(function() {

    var counter = "<?php echo $count;?>"; //$count is the total number of grouped radio buttons

    console.log("Counter: "+counter);
    for(j=1;j<=counter;j++) {   
        var radioname = "optionRadios" + j;
        console.log("RadioName: "+radioname);

        var elem=document.forms['test-form'].elements[radioname];
        len=elem.length-1;
        chkvalue='';

        console.log("Length: "+len);
        for(i=0; i<=len; i++){
            if(elem[i].checked){chkvalue=elem[i].value;}
        }

        console.log("Chkvalue: "+chkvalue);
        if(chkvalue=='') {
            alert('There are still unanswered questions. ' + radioname);
            return false;
        }
    }

    return true;
});

从您的按钮中删除 onclick 属性,并查看正在调试的值。它将帮助您查看您的代码在哪里以及在哪里不工作。试试看,让我们知道控制台输出什么。

【讨论】:

  • 它只返回分组单选按钮的最后一个名称,它仍然提交。
  • 你能复制/粘贴完整的输出,从 Counter: 到 Chkvalue 吗?
【解决方案2】:

JAVASCRIPT

$('input[type=button]').click(function(){
var i= 1 ;
var msgError= "";
while($('input[name=optionRadios'+i+']').length ){

    var is_it_ok = false;
    $('input[name=optionRadios'+i+']').each(function(){
        if($(this).is(":checked")){
            is_it_ok = true;

        }

    });
    if(!is_it_ok){
        msgError += "Erreur : optionRadios"+i; 
    }
    i++;
}
if(!msgError.length){
       alert("ok");
    return true;
}else{
    alert(msgError);
    return false;
}
});
              });

HTML

<form name="test-form">
<input type="radio" name="optionRadios1" value="1" />
<input type="radio" name="optionRadios1" value="1"/>
<input type="radio" name="optionRadios1" value="1"/>
<input type="radio" name="optionRadios2" value="1"/>
<input type="radio" name="optionRadios2" value="1"/>
<input type="radio" name="optionRadios2" value="1"/>
<input type="button" />
</form>

演示:http://jsfiddle.net/VZHGJ/

【讨论】:

  • 它不工作,对不起
  • 它还在提交中...
  • 因为你已经在使用 jquery,所以我用 jquery 重写了所有内容并更新了 jsfiddle
  • 而不是 php/javascript/jquery 奇怪的东西
【解决方案3】:

您是否尝试过使用 jQuery 验证方法? http://jqueryvalidation.org/documentation/

【讨论】:

  • 还没有,我不知道如何通过 jQuery 验证单选按钮
  • @Lix,如果我的声誉不超过 50,我无法发表评论......请不要简单地投反对票......
  • @Christian Burgos 这是一个例子:stackoverflow.com/questions/4147556/…
  • 我给了你一票。别担心点头效果,快乐
  • @nodeffect - 这个限制是有原因的 - 忽略它是我的行动的基础。
猜你喜欢
  • 2012-03-03
  • 2011-06-26
  • 2013-01-08
  • 2020-07-08
  • 2017-11-18
  • 2013-02-10
  • 2012-12-02
  • 2014-03-31
  • 2018-11-23
相关资源
最近更新 更多