我正在努力让 jQuery 脚本正常工作,该脚本将在 RadioButtonList 中获取选定的值。当我无法运行脚本时,我已经做了通常的事情 - 将其他人的剪切并粘贴到空白页面中,让它工作,然后复制到所需的页面中。
但是,我尝试了三种不同的脚本,它们采用不同的方法来定义所选值,但我无法让它们中的任何一个工作,即使我从中获得它们的一些网页上的评论似乎表明它们正在为之工作其他人。
此脚本引发无法识别的表达式错误:
//In head with prior reference to local copy of jquery-1.4.1.js
<script language="javascript">
$(document).ready(function() {
$("#btnSubmit").click(function() {
alert($("#RadioButtonList1").find("input[@checked]").val());
});
});
</script>
//In body
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:RadioButtonList>
<input id="btnSubmit" type="button" value="Submit" />
在下面的代码中,警报框只是给出了“未定义”(注意我已经尝试过使用远程引用和对 jquery-1.4.1.js 的本地副本的引用)
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="2"></asp:ListItem>
</asp:RadioButtonList>
</div>
<input type="button" id="btnTest" value="Uncheck" />
</form>
</body>
<script type="text/javascript">
$('#btnTest').click(function () {
alert($('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val());
});
</script>
</html>
我还在一个基本的 HTML 页面(即不是 asp.net)中尝试了以下内容,但它什么也没做
//In head
$("input[@name='option_layout']").change(
function()
{
if ($("input[@name='option_layout']:checked").val())
$(".group").toggleClass("group_vert");
else
$(".group").toggleClass("group_vert");
$(this).blur();
}
);
//In body
<input type="radio" name="option_layout" value="0" checked="checked" />
<input type="radio" name="option_layout" value="1" />
我尝试将 .change 修改为 .click,因为我知道 IE 中存在带有 .change 的错误,即使我在 IE 和 Fiefox 中都没有成功进行测试。我还尝试将代码包装在 $(document).ready(function() 块中,但没有区别。我还尝试在函数的第一行添加一个警告框,但它再次引发无法识别的表达式错误。
有人知道这里出了什么问题吗?