2012-05-21 49 views
2

我還沒有找到一個帖子,實際上詢問了一個單選按鈕被選中的時刻..我沒有檢查值,只是當某人選擇任何按鈕時。選擇一個單選按鈕後運行一個函數

這是我有:

function turnSubmitBlue() { 
$('.formSubmit').css('backgroundColor','#08aae4'); 
} 

$('input:checkbox').each.change(function(){ 
if ($(this).attr('checked')) { 
checkForThreeRadioBtns() 
}; 
}); 

function checkForThreeRadioBtns() { 
     if (($('input[name=group1]:checked')) && ($('input[name=group2]:checked')) && ($('input[name=group3]:checked'))) { 
      turnSubmitBlue(); 
      } 
} 

是,在頁面加載,沒有什麼選擇我的思維過程,我必須等待,直到一個來自各組表示之前選擇,通過顏色變化上的提交按鈕, 提交。 (如果在選擇每個組的一個收音機之前提交了提交,我確實運行了驗證)。

一旦選擇了三個按鈕,運行該功能以更改提交藍色,顯示其他內容(已經工作得很好)。一旦他們點擊提交,提交按鈕將變灰,並顯示結果。

在這一點上,總會有三個選擇,所以我只需要在發生更改的時刻運行一個函數(通過單擊不同的單選按鈕)來更改提交按鈕。

我的函數checkForThreeRadioBtns()應該確保在所有三個組中都選擇了每個無線電之一,如果是,觸發函數運行turnSubmitBlue()。

我一直在試圖讓jquery檢查一個單選按鈕被改變的時刻,但沒有運氣到目前爲止。

回答

2

嘗試像下面,

$('input:checkbox').change(function(){ 
    if (this.checked) { 
    checkForThreeRadioBtns(); 
    }; 
}); 

也可以嘗試改變你的病情是否像下面,

if ($('input[name=group1]:checked').length && 
     $('input[name=group2]:checked').length && 
     $('input[name=group3]:checked').length) 

完整代碼:

function turnSubmitBlue() { 
    $('.formSubmit').css('backgroundColor','#08aae4'); 
} 

$('input:checkbox').change(function(){ 
    if (this.checked) { 
    checkForThreeRadioBtns(); 
    }; 
}); 

function checkForThreeRadioBtns() { 
    if ($('input[name=group1]:checked').length && 
      $('input[name=group2]:checked').length && 
      $('input[name=group3]:checked').length) { 
     turnSubmitBlue(); 
    } 
} 
0
$('input[type=checkbox]').on('change', function(){ 
    if (this.checked) { 
    checkForThreeRadioBtns() 
    }; 
}); 


function checkForThreeRadioBtns() { 
    if ($('input[name=group1]').is(':checked') && 
     $('input[name=group2]').is(':checked') && 
     $('input[name=group3]').is(':checked')) { 
      turnSubmitBlue(); 
    } 
} 
相關問題