我在这里阅读了大部分问题,但我找不到这个问题的答案,所以我会试一试!
What I'm trying to accomplsih is that when a certain radio is choosen, I want to fetch a certain textbox value. 这就是为什么我需要动态 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;