2010-08-30 33 views
0

我拉着我的頭髮試圖讓一個jQuery腳本工作,它將拾取RadioButtonList中的選定值。當我無法運行腳本時,我做了平常的事情 - 剪切並粘貼其他人的空白頁面,讓它工作,然後複製到所需的頁面。無法讓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相關的bug,即使我在IE和Fiefox中都沒有測試過。我也嘗試在$(document).ready(function()block中包裝代碼,但沒有區別,我也嘗試在函數的第一行添加一個警告框,但它再次引發一個無法識別的表達式錯誤

任何人任何想法是怎麼回事錯在這裏

回答

1

@被廢棄/從attribute selectors在jQuery的1.3刪除,所以你的第一個attept應該是這樣的:?

$(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,只是作爲一個起點:)

+0

使用答案使代碼更清晰 – mharran 2010-08-30 17:11:20

+0

@mharran - 你應該評論如果你有問題...你的問題沒有涉及母版頁,我會指出如何使這項工作,如果你問:) – 2010-08-30 17:31:02

+0

對不起,尼克,我沒有意識到,母版頁可能會導致問題,因爲它與我的其他jQuery代碼工作正常。 另外,我發現自從剛剛給出的解決方案在將jQuery代碼轉移到外部文件時不起作用 - 我仍然在學習這些東西,但我懷疑<%= ... %>不適用於外部文件。 目前,我使用$(「#MainContent_RadioButtonList1」)...這是控件在輸出HTML中獲得的名稱。任何簡單的方法呢? – mharran 2010-08-31 08:15:12

1

謝謝,尼克,你的回答讓我有......最終:)

你的第一個代碼

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

止跌根本不工作,在警告框中給我'未定義'。

你的第二個代碼

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

曾在一個基本的asp.net頁面,但不會在主/子頁面工作,這是我使用這裏。我改成

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

它工作正常。