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