如下HTML代码:
<span>
Yes<input type="radio" name="test1" value=true >
<input type="radio" name="test1" value=false checked="checked">No
</span>
使用jQuery代码去读取radio的值:
var value1 = $('input[name=test1]').val();
//check the value1
if(value1 == true)
//do something
else
//do otherthing
问题来了:如果该radio控件是选择Yes还是No,if条件判断分支总是满足//do otherthing。
原因:value1是字符串类型,即其值为"true"/"false"。
所有需要将jQuery代码修改如下:
var value1 = $('input[name=test1]').val();
//check the value1
if(value1 == "true")
//do something
else
//do otherthing