0

我正在努力让 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() 块中,但没有区别。我还尝试在函数的第一行添加一个警告框,但它再次引发无法识别的表达式错误。

有人知道这里出了什么问题吗?

4

2 回答 2

1

在jQuery 1.3@中已弃用/从属性选择器中删除,因此您的第一次尝试应如下所示:

$(document).ready(function() {
  $("#btnSubmit").click(function() {
    alert($("#RadioButtonList1 input[checked]").val());
  });
});

或者使用选择器更简化:checked一点

$(function() {
  $("#btnSubmit").click(function() {
    alert($("#RadioButtonList1 :checked").val());
  });
});

另外,这就是我认为您在第三次尝试中所追求的,只是作为一个起点:)

于 2010-08-30T10:18:52.297 回答
1

谢谢,尼克,你的回答让我到了那里......最终:)

你的第一个代码

alert($("#RadioButtonList1 input[checked]").val()); 

根本不起作用,在警报框中给我“未定义”。

你的第二个代码

alert($("#RadioButtonList1 :checked").val());

在基本的 asp.net 页面中工作,但不能在我在这里使用的主/子页面中工作。我把它改成

alert($("#<%=RadioButtonList1.ClientID %>").find("input[checked]").val());

它工作正常。

于 2010-08-30T17:13:46.543 回答