1

ここでほとんどの質問を読みましたが、これに対する答えが見つからなかったので、試してみます。

私が達成しようとしているのは、特定のラジオが選択されたときに、特定のテキストボックス値をフェッチしたいということです。そのため、値を選択するテキストボックスを知るために動的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>

ここに私の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

みんなごめんなさいごめんなさい!

HTMLのIDを変更するのを忘れていました。

'getElementsByIDを使用しており、IDとしてグループ名+1を検索していました。IDは

textbox1、textbox2 ....textbox99...それが見つからなかったのも不思議ではありません...

名前とIDを混同しました...ばかげています!!

代わりにIDを使用して名前とimの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>
于 2012-08-26T10:10:21.480 に答える
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 に答える