简体   繁体   English

单选按钮未通过验证

[英]Radio button is not being validated

URL: Form not working URL: 表格不起作用

I have the following script which should validate the form and submit like this example here: form working 我有以下脚本应该验证表单并像此处的示例一样提交: 表单工作

But for some reason it's not working on the first link. 但是由于某种原因,它在第一个链接上不起作用。

My Javascript code remained the same: 我的Javascript代码保持不变:

$("form").submit(function () {
    var html = '';
    var submitme = true;
    $(':radio').each(function() {
        nam = $(this).attr('name');
        if (!$(':radio[name="'+nam+'"]:checked').length) {
            $(':radio[name="'+nam+'"]+label').addClass('error');
            if(html.indexOf(nam) < 0){
                html += nam +' group not checked' + '</br>';
            }
            submitme = false;
        }
        else {
            $(':radio[name="'+nam+'"]+label').removeClass('error');
        }
    });
    if(submitme == false){
        $('.errorDiv').empty().append(html).show().addClass('error');
    }
    else{
        $('.errorDiv').hide();
    }

    return submitme;
});

What I am looking to do is validate the form and make any radio button not chosen RED and not submit the form. 我要做的是验证表单,并使未选中的任何单选按钮变为红色,并且不提交表单。 Once the user selects all the form radios, the form should submit. 一旦用户选择了所有表单收音机,表单就应该提交。

I also had this code which disables the submit button once everything is selected so user cannot submit twice: 我也有这段代码,一旦选择了所有内容,它将禁用提交按钮,因此用户无法提交两次:

$("form").submit(function () {  
        var form = $(this);
        form.find("input[type=submit]").attr("disabled", "disabled")
        form.find("input[type=submit]").attr("value", "Processing...");
        // prevent the user from hitting ENTER on a field to submit again
        form.find("input").attr("readonly", true);
        document.body.style.cursor = "wait";
});

Any idea where to put that in the previous jquery code snippet? 知道在前面的jQuery代码段中将其放在哪里吗?

You have to include that code on the page for it to work. 您必须在页面上包含该代码才能使其正常工作。

Your file linked in as miscScripts.js is invalid javascript. 您以miscScripts.js链接的文件是无效的javascript。

It starts with: 它开始于:

<!--

// function to swap inner DIVs in homepage left content
function showDiv(div_number) {

and end with 并以

});
-->

It SHOULD be: (remove the page level comments markup) 应该是:(删除页面级别的注释标记)

// function to swap inner DIVs in homepage left content
function showDiv(div_number) {

and end with 并以

});

FYI I don't see that "submit" code in any file, which file on your page IS it in? 仅供参考,在任何文件中都看不到该“提交”代码,该文件位于页面中的哪个文件中?

These are the links on the source of that page: 这些是该页面源上的链接:

<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript" src="theScriptsStyles/miscScripts.js"></script>


<script type="text/javascript" src="theScriptsStyles/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="theScriptsStyles/jquery.hoverIntent.minified.js"></script>
<script type="text/javascript" src="theScriptsStyles/jquery.naviDropDown.1.0.js"></script>

EDIT2: OK, I took a moment to look over your code, seemed a bit odd in places, I made some gross assumptions in your objective and reworked your code to produce this: EDIT2:好吧,我花了点时间查看您的代码,在某些地方看起来有些奇怪,我对您的目标做了一些粗略的假设,然后重新编写您的代码以产生此结果:

NOTE: I used the change event to turn off the red errors as the user corrects the form. 注意:当用户更正表格时,我使用change事件关闭了红色错误。 You can use that, or modify as you see fit. 您可以使用它,也可以根据需要进行修改。

$("form#myform").submit(function (e) {
    var myform = $(this); //done this way in case selector has multiple forms
    var invalidCount = myform.data('hasinvalidentries'); //check validation
    if (invalidCount === 0) { //0 not falsy value as 0 set errors check below
        return; // let the event bubble away
    }
    e.preventDefault(); //not check so prevent submit
    var mysubmitButton = myform.find("input[type=submit]");
    mysubmitButton.prop("disabled", true).data('buttontext', mysubmitButton.val()).val('processing...');
    document.body.style.cursor = "wait";
    var html = '';
    var submitme = true;
    $('input[type=radio]').each(function () {
        var nam = $(this).attr('name');
        var radioGroup = myform.find('input:radio[name="' + nam + '"]');
        if (!radioGroup.is(':checked')) {
            radioGroup.next('label').addClass('error');
            if (html.indexOf(nam) < 0) {
                html += nam + ' group not checked' + '<br />';
            }
            submitme = false;
        }
    });

    if (submitme) {
        $('.errorDiv').hide();
        myform.data('hasinvalidentries', 0).submit();
    } else {
        $('.errorDiv').empty().append(html).show().addClass('error');
        return submitme;
    }
});
/* Check to set the radio group as no errors when change one as one will have to be selected.
  Still possible if I manually using code set all to unselected, but acts on users changes at least.
  */
$('form#myform').on('change', 'input:radio', function () {
    var myform = $(this).parents('form'); //done this wa in case selector incudes multiple forms
    var errorCount = myform.find('.error').length;
    if (errorCount) {
        var nam = $(this).attr('name');
        myform.find(':radio[name="' + nam + '"]+label').removeClass('error');
        var mysubmitButton = myform.find("input[type=submit]");
        mysubmitButton.removeProp("disabled").val(mysubmitButton.data('buttontext'));
        errorCount = myform.find('.error').length;
        myform.data('hasinvalidentries', errorCount);
    }
});  

Here is a working example of that code: http://jsfiddle.net/whNtP/ 这是该代码的一个有效示例: http : //jsfiddle.net/whNtP/

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

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