简体   繁体   English

Javascript:使用动态ID获取文本框值

[英]Javascript: Get textbox value with dynamic id

I read most of the questions here but I could not find the answer to this one so I'll give it a shot! 我在这里阅读了大多数问题,但找不到该问题的答案,因此我将给您一个机会!

What I'm trying to accomplsih is that when a certain radio is choosen, I want to fetch a certain textbox value. 我要尝试的是,当选择某个单选按钮时,我想获取某个文本框值。 That's why I need the dynamic ID to know from which textbox I want to pick the value. 这就是为什么我需要动态ID才能知道我要从哪个文本框中选择值。

As you can see below I group my radiobuttons in groups of three. 如您在下面看到的,我将我的单选按钮分为三个组。

The problem is that I can't seem to get the value out of the textbox...! 问题是我似乎无法从文本框中获得价值! No matter what I try it always returns 0(zero) 不管我尝试什么,它总是返回0(零)

This is my html! 这是我的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>

Here my javascript! 这是我的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; 
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 jQuery演示

JavaScript DEMO JavaScript演示

Guys I messed up sorry! 伙计们,我搞砸了对不起!

I had forgot to change the ID in my html. 我忘了更改HTML中的ID。

'Im using getElementsByID, and I was searching for groupname + 1, as the ID. '我正在使用getElementsByID,我正在搜索组名+ 1作为ID。 The ID's were 身份证是

textbox1, textbox2 .... textbox99... No wonder it couldn't find it... textbox1,textbox2 .... textbox99 ...难怪找不到它...

I confused name with ID... stupid I know!! 我把名字和ID弄混了...我知道这很愚蠢!

I rid the texboxes of their name and im using the ID instead... Here's the correct html. 我用它们的ID代替了texbox的名称和即时消息……这是正确的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM