开发者

Check Input text if empty based upon radio button selected

开发者 https://www.devze.com 2023-03-31 06:32 出处:网络
I have two radiobuttons in a group on my page. Based upon radiobutton selected i want to generate an alert.

I have two radiobuttons in a group on my page. Based upon radiobutton selected i want to generate an alert.

var d=GetVal();

function GetVal()
{
var a = null;
var f = document.forms[0];
var e = f.elements["radiogroup"];

for (var i=0; i < e.length; i++)
{
if (e[i].checked)
{
a = e[i].value;
break;
}
}
return a;
}

At the time of form val开发者_C百科idation, It is always returning only first radiobutton value when iam reading it in if else loop.

if (!checkRadio("form","radiogroup")) 
{
alert("none of the option was selected"); 
return false;
}
//if one radio option was selected
else
{
if(d="firstradiovalue"){
alert("first radio selected");
return false;}
else{
if(d="secondradiovalue"){
alert("second radio Selected");
return false;}
}
}

At the time of form submission, even if i choose second option i only get alert - "first radio selected". Any help. Thx in advance.


You need to use == not =:

if(d == "firstradiovalue"){
    alert("first radio selected");

The return value of the expression d = "firstradiovalue" is d itself, which is always true.

0

精彩评论

暂无评论...
验证码 换一张
取 消