2012-09-26 131 views
3

我有一個radibutton列表。通過javascript從radiobuttonlist獲取值?

  <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
       RepeatDirection="Horizontal" RepeatLayout="flow" > 
      <asp:ListItem Selected="True" Text ="Yes" Value="0"></asp:ListItem> 
      <asp:ListItem Text ="No" Value="1"></asp:ListItem> 
      </asp:RadioButtonList> 

並獲得從電臺列表值我有一個函數:

function getvalueofradiolist() { 

     var radiolist = document.getElementById('<%= RadioButtonList1.ClientID %>'); 
     var rblName = radiolist.name; 
     var radio = document.getElementsByName(rblName); 

     for (var x = 0; x < radio.length; x++) { 
      if (radio[x].checked) { 
       alert("Selected item Value " + radio[x].value); 
      } 
     } 
    } 

但它返回undefined。

我寫錯了什麼?

我在點擊按鈕時調用了這個函數。

建議我如果我可以在後臺代碼的幫助下做到這一點,儘管使用Javascript。

回答

13
<form runat="server"> 
<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
      RepeatDirection="Horizontal" RepeatLayout="flow" > 
     <asp:ListItem Selected="True" Text ="Yes" Value="0"></asp:ListItem> 
     <asp:ListItem Text ="No" Value="1"></asp:ListItem> 
     </asp:RadioButtonList> </form> 
<p id="button" onclick="getvalue()">click me</p> 

<script type="text/javascript"> 
function getvalue(){ 
alert($('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val()); 
} 
</script> 
+0

thanx它正在工作 – vikas

0

你可以嘗試:

function getvalueofradiolist() { 

      var radioButtonlist = document.getElementsByName("<%=RadioButtonList1.ClientID%>"); 
      for (var x = 0; x < radioButtonlist.length; x++) { 
       if (radioButtonlist[x].checked) { 
        alert("Selected item Value " + radioButtonlist[x].value); 
       } 
} 
+0

您好繆斯它不起作用 – vikas

+0

Plz幫助我在這 – vikas

+0

建議我如果我能做到這一點,在後面的代碼的幫助下儘量使用javascript – vikas

0

約內直接獲得input元素的dwhat:

function getvalueofradiolist() { 
    var radiolist = document.getElementById('<%= RadioButtonList1.ClientID %>'); 
    var radio = radiolist.getElementsByTagName("input"); 

    for (var x = 0; x < radio.length; x++) { 
     if (radio[x].type === "radio" && radio[x].checked) { 
      alert("Selected item Value " + radio[x].value); 
     } 
    } 
} 
1

有關代碼

var rblName = radiolist.name; 

你甚至不有名稱屬性