日期:2014-05-16  浏览次数:20807 次

jQuery动态给radio赋单值问题
JScript code
$(document).ready(function() {
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "copyUserRole.aspx/GetJsonUserRole",
                data: "{}",
                dataType: "json",
                success: function(result) {
                    try {
                        eval("var userRole=" + result.d);
                        $.each(userRole, function(i) {
                            $("#tdUserGroup").append("<input type='radio' name='userGroup' value='" + userRole[i].roleId + "'/>" + userRole[i].roleName);
                            if ((i + 1) % 5 == 0) {
                                $("#tdUserGroup").append("<br/>");
                            }
                        });
                    }
                    catch (e) {
                        alert(e);
                        return;
                    }
                },
                error: function(result, status) {
                    if (status == "error") {
                        alert(status);
                    }
                }
            });
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "setUserGroup.aspx/GetUserRoleByUserId",
                data: "{userId:'" + '<%=Request["userId"] %>' + "'}",
                dataType: "json",
                success: function(result) {
                    try {
                        if (result.d != null) {
                            document.getElementById("txtaReason").disabled = false;
                            //问题:result.d每次都出来,就是下面这句有时不起作用
                            $("input[@type=radio][@value=" + result.d + "]").attr("checked", true);
                        }
                    }
                    catch (e) {
                        alert(e);
                        return;
                    }
                },
                error: function(result, status) {
                    if (status == "error") {
                        alert(status);
                    }
                }
            });
        });


------解决方案--------------------
你这个是异步的上面的还没创建呢,下面怎么能找到呢
------解决方案--------------------
在第一个ajax的回调里生成完元素后再调用ajax,可以把下面的ajax放到第一个回调的后面,或弄个独立的函数,第一个回调后面再调用那个ajax函数。