Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquery中执行单选按钮验证_Javascript_Jquery - Fatal编程技术网

Javascript 如何在jquery中执行单选按钮验证

Javascript 如何在jquery中执行单选按钮验证,javascript,jquery,Javascript,Jquery,在我的应用程序中,我有两个单选按钮,一个文本框和一个按钮 <input type="radio" name="myRadio" value="red" id="myRadio" checked>Red color <input type="radio" name="myRadio" value="Blue" id="myRadio">Blue color <input type="text" id="txtShareCount" name=

在我的应用程序中,我有两个单选按钮,一个文本框和一个按钮

    <input type="radio" name="myRadio" value="red" id="myRadio" checked>Red color
    <input type="radio" name="myRadio" value="Blue" id="myRadio">Blue color
     <input type="text" id="txtShareCount" name="txtColor"/>
<input type="submit" name="btnsubmit" value="Blue" id="btnsubmit">submit
红色
蓝色
提交
这里,当我选中
蓝色单选按钮时,将出现文本框
我想要的是,当我选中蓝色单选按钮时,我需要对jquery中的文本框进行验证。如果文本框为空,则单击“提交”按钮。它将显示警告消息。我如何在jquery中执行此操作


我在javascript中做到了这一点,它工作得很好,但我需要在jquery中如何在jquery中做到这一点才能获得无线电使用的价值:

$('input[name=myRadio]:checked', '#myForm').val()
或者如果你没有使用表单

$('input[name=myRadio]:checked').val()

要获得收音机的价值,请使用:

$('input[name=myRadio]:checked', '#myForm').val()
或者如果你没有使用表单

$('input[name=myRadio]:checked').val()

对于radiobutton,您可以使用
:checked
选择器-
$(“#我的收音机:checked”)
对于radiobutton,您可以使用
:checked
选择器-
$(“#我的收音机:checked”)
注意:

1-不具有具有相同Id的多个元素

2-使用getElementById时,必须输入id属性

试试这个

函数myFunction(){
if(document.getElementById('myRadioRed').checked==true | | document.getElementById('myRadioBlue').checked==true){
if(document.getElementById('txtShareCount')。值=“”){
警告(“请输入颜色”);
document.getElementById(“txtShareCount”).focus();
返回false;
}
返回false;
}
else{return true;}
}
红色
蓝色
注意:

1-不具有具有相同Id的多个元素

2-使用getElementById时,必须输入id属性

试试这个

函数myFunction(){
if(document.getElementById('myRadioRed').checked==true | | document.getElementById('myRadioBlue').checked==true){
if(document.getElementById('txtShareCount')。值=“”){
警告(“请输入颜色”);
document.getElementById(“txtShareCount”).focus();
返回false;
}
返回false;
}
else{return true;}
}
红色
蓝色

根据传统和文件。如果有多个相同id的元素,js就会出错。因此,不要使用
getElementById()
而是使用
getElementsByName()

像这样的

function validateRadio() {
    var radios = document.getElementsByName("myRadio");
    var formValid = false;

    var i = 0;
    while (!formValid && i < radios.length) {
        if (radios[i].checked) 
           formValid = true;
        i++;        
    }

    if (!formValid) 
       alert("Must check some option!");
    return formValid;
}​
函数validateRadio(){
var radios=document.getElementsByName(“myRadio”);
var formValid=false;
var i=0;
而(!formValid&&i
根据传统和文件。如果有多个相同id的元素,js就会出错。因此,不要使用
getElementById()
而是使用
getElementsByName()

像这样的

function validateRadio() {
    var radios = document.getElementsByName("myRadio");
    var formValid = false;

    var i = 0;
    while (!formValid && i < radios.length) {
        if (radios[i].checked) 
           formValid = true;
        i++;        
    }

    if (!formValid) 
       alert("Must check some option!");
    return formValid;
}​
函数validateRadio(){
var radios=document.getElementsByName(“myRadio”);
var formValid=false;
var i=0;
而(!formValid&&i
首先,不要有多个具有相同Id的元素。首先,不要有多个具有相同Id的元素。使用类