在ajax中的循环附加不起作用

时间:2015-12-07 06:10:47

标签: javascript jquery ajax

我有一个Ajax调用如下

$.ajax({
    type: "POST",
    url: '@Url.Action("SubMenuOrderingGetId", "Admin")',
    data: JSON.stringify({ selectManuId: selectManuId }),
    dataType: "text",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        $('#SubMenuTable12').empty();
        var count = 1;
        $('#SubMenuTable12').append("<tr><th>Id</th><th>Menu Name</th><th>Ordering</th><th>Status</th></tr>");
        var yourArray = $.parseJSON(data);
        if (yourArray != null) {
            var select = '';
            for (var j = 1; j <= 4; j++) {
                select.html = '<option value="' + j + '">' + j + '</option>';
                console.log(j);
            }
            console.log(select);
            for (var i = 0; i < yourArray.length; i++) {
                $('#SubMenuTable12').append(" <tr><td>" + count + "</td><td>" + yourArray[i].MenuName + "</td><td><select name='MenuOrdering211' class='form-control'><option value='Null'>Select Order....</option>'" + select + "'< /select><input type='hidden' class='chkHide1' name='chkHide' value='" + yourArray[i].Id + "'/></td><td><input type='checkbox' class='chkMenuStatus1' name='chkMenuStatus1' value='" + yourArray[i].Id + "' /></td></tr>");
                count++;
            }
        }
    },
    error: function (response) {
        if (response != 1) {
            alert("Error!!!!");
        }
   }
});

在我的ID为SubMenuTable12的表中,我有name='MenuOrdering211'的选项我需要显示j的值(选择顺序,1,2,3,4),因为我的循环将运行4次  我在里面使用了循环它没有用。我也尝试使用另一个追加但显示字符串是空的。

任何帮助都将受到赞赏

2 个答案:

答案 0 :(得分:3)

您正在尝试修改字符串的html属性。此外,您覆盖此值,而不是附加。

尝试以下方法:

if (yourArray != null) {
    var selectHtml = '';
    for (var j = 1; j <= 4; j++) {
        selectHtml += '<option value="' + j + '">' + j + '</option>';
        console.log(j);
    }

    console.log(selectHtml);

    for (var i = 0; i < yourArray.length; i++) {
        $('#SubMenuTable12').append(" <tr><td>" + count + "</td><td>" + yourArray[i].MenuName + "</td><td><select name='MenuOrdering211' class='form-control'><option value='Null'>Select Order....</option>'" + selectHtml + "'< /select><input type='hidden' class='chkHide1' name='chkHide' value='" + yourArray[i].Id + "'/></td><td><input type='checkbox' class='chkMenuStatus1' name='chkMenuStatus1' value='" + yourArray[i].Id + "' /></td></tr>");
        count++;
    }
}

答案 1 :(得分:3)

尝试:

if (yourArray != null) {
            var select = "<select name='MenuOrdering211' class='form-control'><option value='Null'>Select Order....</option>";
            for (var j = 1; j <= 4; j++) {
                select+= '<option value="' + j + '">' + j + '</option>';
                console.log(j);
            }
            select+='</select>';

 for (var i = 0; i < yourArray.length; i++) {
                $('#SubMenuTable12').append(" <tr><td>" + count + "</td><td>" + yourArray[i].MenuName + "</td><td>" + select + "<input type='hidden' class='chkHide1' name='chkHide' value='" + yourArray[i].Id + "'/></td><td><input type='checkbox' class='chkMenuStatus1' name='chkMenuStatus1' value='" + yourArray[i].Id + "' /></td></tr>");
                count++;
            }