2012-04-23 142 views
0

我有這樣的方法測定的值:單選按鈕如何預先選擇值與所述功能

$('.officeapprovalspan').click(function() { 

     var invoicelineid = $(this).data("invoicelineid"); 

     var approval = $(this).data("approval"); 

     $(this).replaceWith('<input type="radio" name="variety" value="null" class="radioapproval" data-invoicelineid=' + invoicelineid + ' /><span class="pleaseapprove">Please Accept</span><br >' + 
        '<input type="radio" name="variety" value="false" class="radioapproval" data-invoicelineid=' + invoicelineid + ' /><span class="onhold">On Hold</span><br >' + 
        '<input type="radio" name="variety" value="true" class="radioapproval" data-invoicelineid=' + invoicelineid + ' /><span class="accepted">Accepted</span>'); 

     $(document).on('change', '.radioapproval', radiohandler); 
    }); 

所以,我有三個單選按鈕。我想實現的是基於變量批准的價值預先選擇適當的單選按鈕。審批將具有空值,錯誤或真實值。

有人能告訴我,你會怎麼寫這給我用replaceWith將它添加

+1

「批准」包含哪些值? – jAndy 2012-04-23 09:15:39

+0

哦,對不起,應該說,無論是虛假的還是真的 – AnonyMouse 2012-04-23 09:16:14

+0

什麼樣的價值預計在'var approval' – Rafay 2012-04-23 09:16:23

回答

2

嘗試使用.html()

var invoicelineid = $(this).data("invoicelineid"); 
var approval = $(this).data("approval"); 

$(this).html(
    '<input type="radio" name="variety" value="null" class="radioapproval" data-invoicelineid=' + invoicelineid + ' /><span class="pleaseapprove">Please Accept</span><br >' + 
    '<input type="radio" name="variety" value="false" class="radioapproval" data-invoicelineid=' + invoicelineid + ' /><span class="onhold">On Hold</span><br >' + 
    '<input type="radio" name="variety" value="true" class="radioapproval" data-invoicelineid=' + invoicelineid + ' /><span class="accepted">Accepted</span>' 
) 
.find(':radio[name="variety"][value="' + approval + '"]') 
.prop('checked', true); 

例如,如果approval變量的值是true的最後一個單選按鈕將被選中。