我认为错误符合var gender=$("input[name='gender']:radio").val();
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function()
{
`$` character is just a shortcut for `window.jQuery`.
$("#sub").click(function()
{
/*fetch username
password*/
var u= $("input:text").val();
var pa=$("input:password").val();
var courses=[];
$.each($("input:checked"),function()
{
courses.push($(this).val());
});
//fetching gender value
var gender=$("input[name='gender']:radio").val();
//gender value not display correctly
$("p").html(**"Username**:"+*u*+"**Password:**"+pa+**"Course** **selected:**"+*courses.join(",")*+"**Gender**"+*gender*);
});
});
</script>
</head>
<body>
<form onsubmit="javascript:return false;">
Username<input type ="text" name="uname" >
Password<input type="password" name="pass" >
Course:
java<input type="checkbox" name="java" value="java">
Php<input type="checkbox" name="php" value="php">
.net<input type="checkbox" name=".net" value=".net">
javascript<input type="checkbox" name="javascript" value="javascript">
Gender:
Male<input type="radio" name="gender" value="male">
Female<input type="radio" name="gender" value="female">
<button id="sub">Submit</button>
</form>
<p></p>
</body>
</html>
答案 0 :(得分:0)
只使用:radio
代替属性选择器
var gender=$("input:radio:checked").val();
即如果您确定只有一个单选按钮。
具体要素:
$("input[name='gender']:checked").val();
答案 1 :(得分:0)
试试这个:
var gender=$("input[name=gender][type=radio]:checked").val();
如果您需要选中元素,请使用:checked
选择器。