2017-03-09 3 views
0

単純なシナリオをオンまたはオフ。のgetラジオボタンの値はjqueryので

JavaScriptがこれまでにいくつかの理由

$('.new_question').each(function() { 
    var active = $(this).parent().next(".radioinput").attr("checked") ? 1 : 0; 
} 

、それは常に値として "0" を生み出します。私はclosestで作業しようとしましたが、siblingsと対処しようとしましたが、nextとなりましたが、何も動作していないようです。

非常に次の.radioinputが必要です。これは直接前に.new_questionに続いています。

私は何が間違っているのか、間違っていますか?

アップデート1

示唆したとして、それが今、この嘘に見えるんので、私は、propattrを変更:

var active = $(this).parent().next(".radioinput").prop("checked") ? 1 : 0; 

しかし、まだ:checkboxいつものように0

+1

変更により、子孫をターゲットに.find()を使用することができますが、 'attrが小道具'に '( "チェックします") ( "checked"); ' – Jer

+0

同じ効果が得られますが、常に0を返します – DasSaffe

+0

'$(this).next()。prop(" checked ")を使う? 1:0' – Mohammad

答えて

1

は、現在の兄弟であります現在の要素に.next()を使用する必要があります。

var active = $(this).next(".radioinput").prop("checked") ? 1 : 0; 
 
console.log(active)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="question_wrapper"> 
 
    <input type="text" class="new_question"> 
 
    <input type="checkbox" class="radioinput" name="new_questionActive[]"> 
 
</div>

OR、あなたが代わりに.next()

var active = $(this).parent().find(".radioinput").prop("checked") ? 1 : 0; 
 
console.log(active)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="question_wrapper"> 
 
    <input type="text" class="new_question"> 
 
    <input type="checkbox" class="radioinput" name="new_questionActive[]"> 
 
</div>

+0

実際に動作します。 claryfingありがとう!親に移動して「次へ」を探すのがうまくいかない理由は分かりません。これを受け入れる。ありがとうございます。 – DasSaffe

関連する問題