2012-08-26 41 views
1

我在這裏閱讀了大多數問題,但找不到這個答案,所以我會給它一個鏡頭!Javascript:使用動態編號獲取文本框的值

我想要幫助的是,當選擇某個無線電時,我想要獲取某個文本框的值。這就是爲什麼我需要動態ID來知道我想從哪個文本框中選取值。

正如你在下面看到的,我將我的單選按鈕分成三組。

問題是,我似乎無法獲得文本框的值...!無論我嘗試什麼,總是返回0(零)

這是我的html!

<asp:RadioButton id="RadioButton1" name="directory" GroupName="sverigemot" value="50" runat="server" Text="Fri tillgång" onclick="calculatePrice()" /> 
    <asp:RadioButton id="RadioButton2" name="directory" GroupName="sverigemot" runat="server" 
     Text="En artikel om dagen(30/mån)" value="25" onclick="calculatePrice()" /> 
    <asp:RadioButton id="sverigemot1" name="choice" GroupName="sverigemot" runat="server" value="0" 
     Text="Skriv antalet artiklar" onclick="calculatePrice()" /> 
    <asp:TextBox ID="textbox1" name="sverigemot1" runat="server" Width="106px"></asp:TextBox> 
     <br /> 
     <asp:RadioButton id="RadioButton4" name="directory" GroupName="handlaihop" value="50" runat="server" Text="Fri tillgång" onclick="calculatePrice()" /> 
    <asp:RadioButton id="RadioButton5" name="directory" GroupName="handlaihop" runat="server" 
     Text="En artikel om dagen(30/mån)" value="25" onclick="calculatePrice()" /> 
    <asp:RadioButton id="handlaihop2" name="choice" GroupName="handlaihop" runat="server" value="0" 
     Text="Skriv antalet artiklar" onclick="calculatePrice()" /> 
    <asp:TextBox ID="textbox2" name="handlaihop1" runat="server" Width="106px"></asp:TextBox> 

這裏是我的javascript!

var selectedDirectory = document.getElementsByTagName('input'); 
var valueOfRadio = 0; 
var y = 0; 

for (var i = 0; i < selectedDirectory.length; i++) 
{ 
    //if the radio button is checked 
    if (selectedDirectory[i].checked && selectedDirectory[i].type == 'radio') 
    { 

     if (selectedDirectory[i].value == '0') { 
      //Puts together the dynamic name 
      var dynamictbname = selectedDirectory[i].name + "1"; 
      //Checks the name 
      alert(dynamictbname); 
      //Stores the actual textbox 
      var dynamictb = document.getElementById(dynamictbname); 
      alert('Value in textbox is: ' + parseInt(dynamictb.value)); 


      if (parseInt(dynamictb.value) == null) { 
       alert('Textboxen är null'); 
      } 
      else { 
       var tbvalue = parseInt(dynamictb.value); 
       valueOfRadio += parseInt(document.getElementsByName(dynamictbname)); 
       alert('Name and Value in textbox is: ' + dynamictbname + ' = ' + tbvalue); 
       valueOfRadio += parseInt(selectedDirectory[i].value); 
       y++; 
      } 
     } 
     else { 

      valueOfRadio += parseInt(selectedDirectory[i].value); 
      alert(valueOfRadio); 

     } 

    } 
} 

var divobj = document.getElementById('thePrice'); 
divobj.innerHTML = "value" + valueOfRadio; 

回答

0
onchange="setValue(this);" 

function setValue(node){ 
    var txtBoxValue = document.getElementById(node.value).value; 
    alert(txtBoxValue); 
}​ 

$('input[name="directory"]').change(function(){ 
var selector = '#' + $(this).val(); 
var txtBoxValue = $(selector).val(); 
}) 

jQuery DEMO

JavaScript DEMO

+0

它的jQuery代碼..他需要它在純javascript – AdityaParab

+0

@Maverick:哎呀,現在改變它。 –

+0

我搞砸了!張貼我自己的答案感謝閱讀並通過你的時間..! – 8bitcat

0

傢伙我搞砸了對不起!

我忘了更改我的HTML中的ID。

'我使用getElementsByID,我正在搜索組名+ 1作爲ID。該ID的是

TextBox1中,TextBox2中.... textbox99 ......難怪它無法找到它......

我ID混淆名稱...愚蠢的我知道!

我擺脫了他們的名字和即時通訊使用ID的texboxes ... 這裏是正確的HTML。

<asp:RadioButton id="RadioButton1" name="directory" GroupName="sverigemot" value="50" runat="server" Text="Fri tillgång" onclick="calculatePrice()" /> 
    <asp:RadioButton id="RadioButton2" name="directory" GroupName="sverigemot" runat="server" 
     Text="En artikel om dagen(30/mån)" value="25" onclick="calculatePrice()" /> 
    <asp:RadioButton id="RadioButton3" name="choice" GroupName="sverigemot" runat="server" value="0" 
     Text="Skriv antalet artiklar" onclick="calculatePrice()" /> 
    <asp:TextBox ID="sverigemot1" runat="server" Width="106px"></asp:TextBox> 
     <br /> 
     <asp:RadioButton id="RadioButton4" name="directory" GroupName="handlaihop" value="50" runat="server" Text="Fri tillgång" onclick="calculatePrice()" /> 
    <asp:RadioButton id="RadioButton5" name="directory" GroupName="handlaihop" runat="server" 
     Text="En artikel om dagen(30/mån)" value="25" onclick="calculatePrice()" /> 
    <asp:RadioButton id="RadioButton6" name="choice" GroupName="handlaihop" runat="server" value="0" 
     Text="Skriv antalet artiklar" onclick="calculatePrice()" /> 
    <asp:TextBox ID="handlaihop1" runat="server" Width="106px"></asp:TextBox> 
相關問題