2012-08-07 210 views
0

我有一個小窗體。當所有的單選按鈕被選中時,我想啓用提交按鈕。下面需要啓用提交按鈕

是我的代碼的html

<form action="#" id="form1" method="post"> 
       <ul> 
        <li> 
         <h3>Here’s what I want:<strong class="result1"></strong></h3> 
         <div> 
         <span><input type="radio" value="Cash Back rewards" name="want" id="01" /><label for="01">Cash Back rewards</label></span> 
         <span><input type="radio" value="Travel rewards" name="want" id="02" /><label for="02">Travel rewards</label></span> 
         <span><input type="radio" value="Gas rewards" name="want" id="03" /><label for="03">Gas rewards</label></span> 
         <span><input type="radio" value="Shopping rewards" name="want" id="04" /><label for="04">Shopping rewards</label></span> 
        </div> 
        </li> 

        <li> 
         <h3>Here’s my credit story: <strong class="result2"></strong></h3> 
         <div> 
         <span><input type="radio" value="I’m new to credit" name="story" id="05" /><label for="05">I’m new to credit</label></span> 
          <span><input type="radio" value="I pay my bills on time" name="story" id="06" /><label for="06">I pay my bills on time</label></span> 
         <span><input type="radio" value="I’ve had credit issues in the past" name="07" id="issues" /><label id="07">I’ve had credit issues in the past</label></span> 
         </div> 
        </li> 

        <li> 
         <h3>Here’s what I carry:</h3> 
         <span><input type="radio" value="I have a credit card with a good record" name="carry" id="08" /><label for="08">I have a credit card with a good record</label></span> 
         <span><input type="radio" value="I don’t have a credit card with a good record" name="carry" id="09" /><label for="09">I don’t have a credit card with a good record</label></span> 

        </li> 
       </ul> 
       <input type="submit" value="" name="" class="find" /> 
       </form> 

我在JavaScript的弱,請告訴我。

還有一件事情,如果李不修復它會動態生成,所以我將不得不做。基本上每一個李是一個隊列,單選按鈕是選項。所以這個問題會被動態生成,它可以是任何沒有。它沒有修復

回答

0

在你的標記中有4組單選按鈕,你可以檢查選定的單選按鈕的長度是否等於廣播組的長度,如果是這樣,啓用提交按鈕,請嘗試以下操作:

$(document).on('change', 'input[type=radio]', function(){ 
    if ($('input[type=radio]:checked').length == 4) { 
     $('input[type=submit]').prop('disabled', false) 
    } 
}) 

DEMO

請注意story組中的第三個元素具有不同的名稱屬性。

<span><input type="radio" value="I’m new to credit" name="story" id="05" /><label for="05">I’m new to credit</label></span> 
<span><input type="radio" value="I pay my bills on time" name="story" id="06" /><label for="06">I pay my bills on time</label></span> 
<span><input type="radio" value="I’ve had credit issues in the past" name="07" id="issues" /><label id="07">I’ve had credit issues in the past</label></span> 
               --------------------------^ 
+0

** **大膽的一兩件事,如果李不解決它會動態生成,因此,我將不得不做。 基本上每個李是一個隊列和單選按鈕是選項。所以這個問題會被動態生成,它可以是任何沒有。它沒有修復 – 2012-08-07 07:29:45

+0

@VikasSharma抱歉,您應該爲此發佈另一個問題。但等一下我會更新我的答案。 – undefined 2012-08-07 07:31:11

+0

@VikasSharma你應該委託事件,嘗試更新的事件。 – undefined 2012-08-07 07:32:35

1
$(':radio').on('change', function(e) { 
    var len = $(':radio:checked').length; 
    if(len === 3) { 
    $('input[type=submit].find').prop('disabled', false); 
    } 
}); 

DEMO

+0

** bold **如果li沒有修復它會動態生成另外一件事,那麼我將不得不做的事情 – 2012-08-07 07:25:38

+0

'3'可以用'$('#form1 li')替換'length'如果元素的數量不是固定的。請注意,第二個'li'的最後一個單選按鈕與其他的'name'屬性沒有相同的名稱。這個答案假設 - 合理 - 這是錯誤的。 – 2012-08-07 07:49:20

0

再或者:

$('input[type=radio]').on('change', function(){ 
    if(
     $('input[name=want]').is(':checked') && 
     $('input[name=story]').is(':checked') && 
     $('input[name=carry]').is(':checked') 
    ){ 
     $('input[type=submit]').prop('disabled', false) 
    } 
});