2013-10-17 25 views
0

我無法讓我的jQuery代碼自動選擇一個無線電框。 這裏是我的html:jQuery自動選擇無線電框不工作

<div class="formField rsform-block rsform-block-existingcustomer" style="margin-bottom: -22px;"> 

    <!--<input name="form[existingCustomer]" type="radio" value="Yes" id="existingCustomer0" /><label for="existingCustomer0">Yes</label><input checked="checked" name="form[existingCustomer]" type="radio" value="No" id="existingCustomer1" /><label for="existingCustomer1">No</label><br/> 
    <span id="component100" class="formNoError">Please tell us if you're an existing customer.</span>--> 

    Are you an existing client?<br> 
    <label for="existingCustomer0" class="radio"><span class="icon"></span><span class="icon-to-fade"></span>Yes 
    <input name="form[existingCustomer]" type="radio" value="Yes" id="existingCustomer0" class="addRadio"> 
    </label> 
    <label for="existingCustomer1" class="radio checked"><span class="icon"></span><span class="icon-to-fade"></span>No 
    <input checked="checked" name="form[existingCustomer]" type="radio" value="No" id="existingCustomer1" class="addRadio" style="display:none;"> 
    </label> 
</div> 

,這裏是jQuery代碼的片段,是應該做的是:

if(aaid) { 

var num_one = aaid; 
jQuery('input[value="Yes"]').prop('checked', true); 

有誰看到這個問題?我正在嘗試自動選擇「是」複選框,以便它會激活下一個創建下拉菜單的部分。

在此先感謝! :)

+0

你是什麼意思,這個自動選擇?即在頁面加載時,這個無線電被檢查。或者你只是想點擊選擇它。 – super

+0

在頁面加載時會被檢查。 –

回答

1

試試這個:

$(document).ready(function() {     
    $('input:radio[name="form[existingCustomer]"][value="Yes"]').attr('checked',true); 
    //OR 
    $('input:radio[name="form[existingCustomer]"][value="Yes"]').prop('checked',true); 
    }); 

Example

+0

不幸的是,沒有工作。然而,我確實將你的'$'改爲jQuery,但那不應該緊。它也顯示在控制檯日誌中沒有錯誤...奇怪。 –

+0

檢查示例。 – super

+0

你們的例子似乎工作,...唯一的區別是你重組的HTML。我有用於選擇的自定義按鈕類。我試試玩。 –

0

我在這裏看到你的代碼一對夫婦的問題。 1.輸入hmtl沒有正確的結束/結束標記 2.不確定爲什麼你將它包裝在標籤 3.確保將jquery代碼放入文檔中,以便在加載頁面時檢查radioobox。 4.在你的html代碼中,你預先設置了No radio來檢查。這是故意的嗎?它看起來像你設置爲no,然後使用jquery將其設置回yes。

無論如何,請嘗試attr而不是prop。像這樣的東西。

$('input:radio[value="Yes"]').attr('checked', true);