0

次のような一連のラジオボタンがあります。

<input type="radio" name="r1" value="1" />
<input type="radio" name="r1" value="2" />
<input type="radio" name="r1" value="3" />
<input type="radio" name="r1" value="4" />
<input type="radio" name="r1" value="-1" />

<input type="radio" name="r2" value="1" />
<input type="radio" name="r2" value="2" />
<input type="radio" name="r2" value="3" />
<input type="radio" name="r2" value="4" />
<input type="radio" name="r2" value="-1" />

<input type="radio" name="r3" value="1" />
<input type="radio" name="r3" value="2" />
<input type="radio" name="r3" value="3" />
<input type="radio" name="r3" value="4" />
<input type="radio" name="r3" value="-1" />

私がする必要があるのは、チェックを実行して、選択したラジオ ボタンのいずれかに値があるかどうかを確認しequal to or less than 2 but greater than zero、コードを実行するか、値があるgreater than 2場合はさらにコードを実行することです。

以前は javascript を使用してこれを達成しましたが、長くて骨の折れるプロセスでした。これを達成するためのjQueryの効率的な方法はありますか?

4

5 に答える 5

3
$('input[type="radio"]:checked').each(function() {
    if (this.value > 0 && this.value <= 2) {
       // do something if the value is less than zero and below or equal to two
    }else if (this.value > 2) {
       // do something else if the value is greater than two
    }
});
于 2013-07-24T13:16:32.770 に答える
0
$('input[type=radio]').on('change', function(){
  if((this.val() > 0) && (this.val() <=2)) {
    //Todos
   }
  else if(this.val() > 2) {
   //Todos
   }    
});
于 2013-07-24T13:18:15.600 に答える
0
var $lessOrEqual2AndGreatZero = $('input[type="radio"]:checked').filter(function(elem){
    return ($(elem).val() > 0 && $(elem).val() <= 2);
});


if ($lessOrEqual2AndGreatZero.length){
   // code
} else {
    var $greaterThanTwo = $('input[type="radio"]:checked').filter(function(elem){
        return ($(elem).val() > 2);
    });
    if ($greaterThanTwo.length){
        // more code
    }
}
于 2013-07-24T13:19:53.720 に答える
0

あなたの質問を理解できたら:

var check = $(':radio:checked').is(function () {
    return this.value <= 2 && this.value > 0
});

if(check)
   //at least one radio button which is checked as value > 0 but less or equal to 2
于 2013-07-24T13:21:27.153 に答える