2013-04-24 28 views
0

我有2個音頻按鈕。一個單選按鈕具有3sub級別的單選按鈕。 例如:jquery插件的兒童單選按鈕驗證

<input type="radio" name="catagory" /> 
        <label class="" for="">A</label> 
    <input type="radio" class="btn-redio" name="catagory" /> 
        <label class="" for="">B</label> 

一旦我cheked單選按鈕B我會得到單選按鈕的3sub水平。 我想驗證,如果用戶點擊B,那麼需要檢查是否有任何子收音機被選中。

子水位意味着:如果單擊按鈕B,則只有另一3radio按鈕將被JS

感謝

顯示

AZ

+2

你是什麼意思_3sub級別單選按鈕_?你還沒有嘗試過任何東西,至少發佈了所有相關的標記。 – undefined 2013-04-24 11:41:31

+0

一旦點擊了按鈕B,那麼3個子單選按鈕就會被JS追加 – azeem 2013-04-24 11:45:50

回答

1

如果我理解正確的,這是你的情況:

<input type="radio" name="parent_radio1" value="..." /> 
<input type="radio" name="sub_radio1_1" value="..." /> 
<input type="radio" name="sub_radio1_2" value="..." /> 
<input type="radio" name="sub_radio1_3" value="..." /> 

<input type="radio" name="parent_radio2" value="..." /> 
<input type="radio" name="sub_radio2_1" value="..." /> 
<input type="radio" name="sub_radio2_2" value="..." /> 
<input type="radio" name="sub_radio2_3" value="..." /> 

如果是這種情況,你可以使用類似的東西(未測試):

var form = $('#someForm'); 
form.submit(function(event){ 
    var parent_radios = $('input[type=radio][name^="parent_radio"]'); 
    parent_radios.each(function(){ 
    var parent = $(this); 

    if (parent.prop('checked') !== true) return; // Parent not selected, no validation needed 

    var child_selector = parent.attr('name').replace('parent_radio', ''); 
    var childs = $('input[name^="sub_radio' + child_selector + '_"]:checked'); // Select only the selected ones 

    if (childs.length == 0) { 
     event.preventDefault(); // No sub radio has been selected for this parent, cancel submit 
     // Do some other stuff here like show error message 
    } 
    }); 

});