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);
    });

所以,我有三个单选按钮。我想要实现的是基于变量批准的值预选适当的单选按钮。Approval 的值为 null、false 或 true。

有人可以告诉我你会如何写这个,因为我用replaceWith添加它

4

1 回答 1

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最后一个单选按钮,则将被选中。

于 2012-04-23T09:22:59.670 回答