1
<input type="radio" name="animal" id="cat" value="Cat" />
                <label for="cat">Cat</label>

            <input type="radio" name="animal" id="radio-choice-2" value="choice-2"/>
            <label for="radio-choice-2">Dog</label>

            <input type="radio" name="animal" id="radio-choice-3" value="choice-3"  />
            <label for="radio-choice-3">Hamster</label>

            <input type="radio" name="animal" id="radio-choice-4" value="choice-4"  />
            <label for="radio-choice-4">Lizard</label>
            <input type="button" value="Get Radio Button Value" onclick="getValue();"

コードを書いたボタンをクリックして、選択したラジオボタンの値を取得したい

function getValue () {
    alert("Value is :"+$('input[name=animal]:checked').val());  
    }

しかし、それは機能せず、未定義になります

アップデート:

私はJqueryのjsとjquerymobileをjqueryに使用しましたが、モバイルアプリに使用しているため、両方のjsライブラリが必要なので、2番目のケースでは機能しませんでした。

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>

【解決済み】

4

2 に答える 2

1

これを試してください $("#myform input[type='radio']:checked").val();

また

var myRadio = $('input[name=myRadio]');

//find() ではなく、filter() 関数を使用します。(find() は子要素を検索するためのもので、 //filter() は選択範囲の最上位要素を検索します。)

var checkedValue = myRadio.filter(':checked').val();

于 2012-05-02T10:07:06.767 に答える
1

こんにちは、ラジオボタンをチェックせずに値を取得していると思います。ページをロードし、ラジオボタンのいずれかを選択してから getValue 関数を呼び出します

または、デフォルトで選択されている任意のラジオ ボタンを配置することもできます。

例えば

最初のラジオボタンを

<input type="radio" name="animal" id="cat" value="Cat" checked="true"/>
                <label for="cat">Cat</label>
于 2012-05-02T09:39:25.937 に答える