0

チェックボックスを含むフォームがあり、最初のチェックボックスがユーザーによってチェックされているかどうかを確認したい:

<form id= 'other_langs'>
    <input type= 'checkbox' name= 'yes'> I speak other languages <br>
    <input type= 'checkbox' name= 'no'> I do <b>not</b> speak any other language <br>   
</form>

そして私はjQuery関数を書きました

$('#other_langs').click(function(){
  if(('#other_langs').first().prop('checked') == true){alert('ok');}
});

アラート (または何でも) は表示されません。.is(':checked') も試しましたが、同じです。私も試しました

if(('#other_langs input[name="yes"]').prop('checked') == true){alert('ok');}

jQuery バージョン 1.9.1 を使用しており、Firefox と Chrome の両方を試しました。

4

2 に答える 2

0

これをしている間:

$('#other_langs').first()

最初#other_langsがチェックされているかどうかを確認しています。#other_langsはフォームです。.children() の最初の子を取得するために行う必要があります#other_langs

 $('#other_langs').children().first()
于 2013-06-16T14:37:28.970 に答える
0

$あなたは2番目の例を見逃しました(最初の例でも、間違ったアプローチで)-

if($('#other_langs input[name="yes"]').prop('checked') == true){
  alert('ok');
}

デモ---> http://jsfiddle.net/zKkXp/

最初の例を次のように機能させることができます-

if($('#other_langs input').first().prop('checked') == true){
  alert('ok');
}

デモ--> http://jsfiddle.net/zKkXp/1/

于 2013-06-16T14:32:30.093 に答える