我在這裏閱讀了大多數問題,但找不到這個答案,所以我會給它一個鏡頭!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;
它的jQuery代碼..他需要它在純javascript – AdityaParab
@Maverick:哎呀,現在改變它。 –
我搞砸了!張貼我自己的答案感謝閱讀並通過你的時間..! – 8bitcat