1

我在这里阅读了大部分问题,但我找不到这个问题的答案,所以我会试一试!

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; 
4

2 回答 2

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 演示

JavaScript 演示

于 2012-08-26T09:47:52.837 回答
0

伙计们我搞砸了对不起!

我忘了更改我的 html 中的 ID。

'我正在使用 getElementsByID,并且我正在搜索 groupname + 1 作为 ID。身份证是

textbox1, textbox2 .... textbox99... 难怪找不到...

我把名字和身份证混淆了……我知道愚蠢!

我摆脱了他们的名字的texboxes,而是使用ID来代替......这是正确的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>
于 2012-08-26T10:10:21.480 回答