【问题标题】:Cannot get jQuery and RadioButtonList to Work无法让 jQuery 和 RadioButtonList 工作
【发布时间】:2010-08-31 01:13:46
【问题描述】:

我正在努力让一个 jQuery 脚本正常工作,该脚本将在 RadioButtonList 中获取选定的值。当我无法运行脚本时,我已经做了通常的事情 - 将其他人的脚本剪切并粘贴到空白页面中,让它工作,然后复制到所需的页面中。

但是,我尝试了三种不同的脚本,它们采用不同的方法来定义所选值,但我无法让它们中的任何一个工作,即使我从中获取它们的某些网页上的 cmets 似乎暗示它们正在为其他人工作。

此脚本引发无法识别的表达式错误:

//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,因为我知道 .change 在 IE 中存在错误,即使我在 IE 和 Fiefox 中都没有成功测试过。我还尝试将代码包装在 $(document).ready(function() 块中,但没有区别。我还尝试在函数的第一行添加一个警告框,但它再次引发无法识别的表达式错误。

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

【问题讨论】:

    标签: jquery radiobuttonlist


    【解决方案1】:

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

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

    或者使用the :checked selector更简化一点:

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

    另外,here's what I think you're after in the third attempt,只是作为一个起点:)

    【讨论】:

    • @mharran - 如果您遇到问题,您应该发表评论...您的问题不涉及母版页,如果您问的话,我会指出如何进行这项工作:)跨度>
    • 对不起,尼克,我没有意识到母版页可能会导致问题,因为它与我的 jQuery 代码的其余部分工作正常。此外,我发现,当我将 jQuery 代码传输到外部文件时,我刚刚给出的解决方案不起作用 - 我仍在学习这些东西,但我怀疑 没有不能从外部文件工作。目前,我正在使用 $("#MainContent_RadioButtonList1 ")... 这是控件在输出 HTML 中获得的名称。有什么简单的方法吗?
    【解决方案2】:

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

    你的第一个代码

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

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

    你的第二个密码

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

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

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

    而且效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多