여기에서 대부분의 질문을 읽었지만이 답변을 찾을 수 없으므로 한번 시도해 보겠습니다.자바 스크립트 : 동적 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;
jQuery 코드입니다. 그는 순수한 자바 스크립트로 작성해야합니다. – AdityaParab
@Maverick : 죄송합니다. 지금 변경하십시오. –
나는 또한 어지럽게 ..! 처음부터 끝까지 읽고 시간을내어 주셔서 감사합니다. – 8bitcat