0

ページの読み込みでいくつかのラジオ ボタン リスト コントロールを動的に作成し、ブラウザーにレンダリングする ASPX ページがあります。各ラジオ ボタン リストに 2 つのリスト項目があります。 n.現在、私のJavaスクリプトコードで、どのラジオボタンがクリックされているかを知りたいです。私はすでにjQueryを使用しています.これを処理する簡単な方法はありますか?

ブラウザにレンダリングされた私のHTMLは

<table border="0" id="tblQuestions">
    <tr>
        <td>Do you have a valid passport ?</td>
    </tr><tr>
        <td><table id="answer-1" border="0">
            <tr>
                <td><input id="answer-1_0" type="radio" name="answer-1" value="1" /><label for="answer-1_0">Yes</label></td>
            </tr><tr>
                <td><input id="answer-1_1" type="radio" name="answer-1" value="0" /><label for="answer-1_1">No</label></td>
            </tr>
        </table></td>
    </tr><tr>
        <td>Are you married ?</td>
    </tr><tr>
        <td><table id="answer-2" border="0">
            <tr>
                <td><input id="answer-2_0" type="radio" name="answer-2" value="1" /><label for="answer-2_0">Yes</label></td>
            </tr><tr>
                <td><input id="answer-2_1" type="radio" name="answer-2" value="0" /><label for="answer-2_1">No</label></td>
            </tr>
        </table></td>
    </tr>
</table>

前もって感謝します

4

2 に答える 2

3

次のコードは具体的にあなたの質問に答えるものではありませんが、役立つかもしれません。変更しますが、今は時間がありません。

私はこれを試験で使用して、特定の質問に選択された回答がないことをユーザーに警告します。

質問は、プレーンなxhtmlを発行するサーバーコントロールを使用して動的に生成されます。すべてのオプションに同じ名前(Q1、Q2 ...)を付け、(Q1a、Q1b ...)のようにIDを付けます。

目的に合わせて変更するには、jループで選択したオプションのリストを作成します。つまり、「break」ステートメントがある場所に名前と値のペアを追加します。

// Grabs all inputs - radio, checkbox, text, buttons and lists -sticks them in an array
allInputs = document.getElementsByTagName("input");
var last = "NameUnlikelyToBeUsedAsAnElementName";

// walk through the array
for (i = 0; i< allInputs.length; i++)
{
    var input = allInputs[i];
    if (input.name == last) continue; // if this object name is the same as the last checked radio, go to next iteration


    // checks to see if any  one of  similarly named radiobuttons is checked 
    else if (input.type == "radio" )
    {    
        last = input.name;  
        var radios = document.getElementsByName(input.name);
        var radioSelected=false;

         //iterate over question options
        for (j=0; j < radios.length; j++)
        {
            if(radios[j].checked)
            {
               radioSelected=true;
               break; // Found it, proceed to next question 
            }
        }
        if (!radioSelected) // no option selected
        {       // warn user, focus question
            alert("You did not answer question " + input.id.substring(0,input.id.length-1));
            input.focus();
            return false;
        }                   
    }

}

return true;
于 2009-05-27T13:27:18.113 に答える
0

これは、ページに jquery が既に含まれていることを前提としています。

ラジオ ボタン アイテムにクラスを定義します。基本的に、クライアント側の HTML は次のようになります。<input id="answer-2_1" type="radio" name="answer-2" value="0" class="myrdo" />

次に、js コードで、クラスにイベント ハンドラーを接続するだけです。

$(".myrdo").bind("click",function(){if($(this).attr("checked")==true) {alert($(this).val);} });

上記のコマンドは、選択したラジオ ボタンの値をアラートするだけです。

于 2009-05-27T11:38:33.620 に答える