2012-08-26 6 views
1

여기에서 대부분의 질문을 읽었지만이 답변을 찾을 수 없으므로 한번 시도해 보겠습니다.자바 스크립트 : 동적 ID를 사용하여 텍스트 상자 값 가져 오기

내가 달성하려고하는 것은 특정 라디오가 선택되면 특정 텍스트 상자 값을 가져 오려고한다는 것입니다. 그래서 내가 값을 고를 텍스트 상자를 알기 위해 동적 ID가 필요합니다.

아래에서 알 수 있듯이 3 개의 그룹으로 라디오 버튼을 그룹화 할 수 있습니다.

문제는 텍스트 상자에서 값을 가져올 수 없다는 것입니다 ...! 아무리 노력해도 항상 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> 

여기 내 자바 스크립트!

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 코드입니다. 그는 순수한 자바 스크립트로 작성해야합니다. – AdityaParab

+0

@Maverick : 죄송합니다. 지금 변경하십시오. –

+0

나는 또한 어지럽게 ..! 처음부터 끝까지 읽고 시간을내어 주셔서 감사합니다. – 8bitcat

0

얘들 아 미안 엉망!

내 HTML에서 ID를 변경하는 것을 잊어 버렸습니다.

'getElementsByID를 사용하여 ID로 groupname + 1을 (를) 검색했습니다. 아이디 년대는

으로 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> 
관련 문제