简体   繁体   English

表单验证单选按钮?

[英]Form validation radio button?

I got a form that's already using validation but I want to also include validation for the radio buttons. 我有一个已经在使用验证的表单,但是我还想包括单选按钮的验证。 This is the top of the html form showing its using the onSubmit="" already. 这是html表单的顶部,已经显示了使用onSubmit =“”的形式。

<form id="form_603298" class="appnitro" method="post" action="" onSubmit="javascript:return validate_form();">
<fieldset>

And i added this to the bottom of the html form: 我将其添加到html表单的底部:

<fieldset>
    <legend>Status</legend>
    <input id="element_14_1" name="element_14" type="radio" value="1" />
    <label for="element_14_1">Active</label>
    <input id="element_14_2" name="element_14" type="radio" value="2" />
    <label for="element_14_2">Inactive</label>
    </fieldset>

Now I am trying to include the radio button validation as part of the rest of the validation script. 现在,我尝试将单选按钮验证作为验证脚本其余部分的一部分。 I tried to include this at the bottom of the validation scripts: 我试图将其包括在验​​证脚本的底部:

<!--================Validate drop down options==========================-->
<script type="text/javascript" language="javascript">
function validate_dropdown(){
    var dropdown = document.getElementById('element_11');
    var dropdown = document.getElementById('element_12');
    var dropdown = document.getElementById('element_13');
    if(dropdown.selectedIndex==0){
        alert("Please select drop down options");

    return false; 
    }else{

    return true;
    }
}

<!--================Validation start and end dates==========================--> 
    $(document).ready(function() {
        $("#dataStart").datepicker({
            minDate: 1,
            onSelect: function(theDate) {
                $("#dataEnd").datepicker('option', 'minDate', new 
Date(theDate));
            },
            beforeShow: function() {
                $('#ui-datepicker-div').css('z-index', 9999);
            },
            dateFormat: 'yy/mm/dd'
        });
        $("#dataEnd").datepicker({
            beforeShow: function() {
                $('#ui-datepicker-div').css('z-index', 9999);
            },
            dateFormat: 'yy/mm/dd'
        });

    });

<!--================Validate radio buttons==========================-->

if(document.getElementById('element_14_1').checked) {
//Active radio button is checked
}else if(document.getElementById('element_14_2').checked) {
//Inactive is checked
}

This does not work. 这是行不通的。 Can anyone point me in the right direction. 谁能指出我正确的方向。 I am new to this so I kindly ask that any explanation be simple to understand. 我对此并不陌生,所以请您将任何解释都易于理解。

Judging from the script you provided I would assume that you have included the jQuery library on your page. 从您提供的脚本来看,我假设您已在页面上包含jQuery库。 If so, replace this part of your script: 如果是这样,请替换脚本的这一部分:

if(document.getElementById('element_14_1').checked) {
//Active radio button is checked
}else if(document.getElementById('element_14_2').checked) {
//Inactive is checked
}

With this: 有了这个:

$(':radio').change(function() {
    if($('#element_14_1').is(':checked')) { 
      alert("Active radio checked"); 
    }
    if ($('#element_14_2')) {
     alert("Inactive radio checked"); 
    }
});

This simply adds a change event to all radio buttons and within the event we check if your radio buttons are checked. 这只是向所有单选按钮添加了一个更改事件,并且在事件内我们检查是否已选中您的单选按钮。

This will show an alert a radio button is fixed but you can replace the alert with what ever you like. 这将显示一个警报,单选按钮已固定,但您可以将警报替换为所需的警报。

If this is not what you after, please let me know and I'll adjust my answer :P 如果这不是您想要的,请让我知道,我将调整答案:P

function checkStatus(evt) {
    var val = $("input[name=element_14]:checked").val();
    if (val == 'Active') {
        alert(val);//Active is checked
    } else {
        alert(val);//in active is checked
    }
}

$('input[name=element_14]:radio').change(checkStatus);

checkStatus();

This will resolve your issue.. 这样可以解决您的问题。

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

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