for loop inside the append is not working

こ雲淡風輕ζ 提交于 2019-12-12 09:41:06

问题


below is my code.

$("<table/>",{"cellspacing":"0","cellpadding":"0","border":"0","width":"100%"})
.append(
$("<tbody/>")
.append(function(){

    options=["ONE","TWO","THREE","FOUR"];
    $.each(options, function(val) {
        return ($("<tr/>")
        .append(
            $("<td/>").html("4/6/2013 10:41"),
            $("<td/>").html("5/6/2013 10:42"),
            $("<td/>").html(val),
            $("<td/>").html("<img src='pdf_16x16.png'/>"),
            $("<td/>").html("<a href='#'>Re-Upload Documents</a>")
        ));
    })

})
).appendTo("body");

for loop inside the append is not working.


回答1:


The problem is because you are not returning anything from the append function, only the each loop within it. Try this:

$("<tbody/>").append(function(){
    options = ["ONE","TWO","THREE","FOUR"];
    var $container = $('<table></table>');

    $.each(options, function(val) {
        $container.append($("<tr/>").append(
            $("<td/>").html("4/6/2013 10:41"),
            $("<td/>").html("5/6/2013 10:42"),
            $("<td/>").html(val),
            $("<td/>").html("<img src='pdf_16x16.png'/>"),
            $("<td/>").html("<a href='#'>Re-Upload Documents</a>")
        ));
    });

    return $container.html();
});

Example fiddle




回答2:


Try

var tbody = $("<tbody/>").appendTo($("<table/>", {
    "cellspacing" : "0",
    "cellpadding" : "0",
    "border" : "0",
    "width" : "100%"
}).appendTo("body"));

options = ["ONE", "TWO", "THREE", "FOUR"];
$.each(options, function(key, val) {
    return tbody
    .append($("<tr/>")
            .append($("<td/>").html("4/6/2013 10:41"))
            .append($("<td/>").html("5/6/2013 10:42"))
            .append($("<td/>").html(val))
            .append($("<td/>").html("<img src='pdf_16x16.png'/>"))
            .append($("<td/>").html("<a href='#'>Re-Upload Documents</a>")));
})

Demo: Fiddle




回答3:


Try like

$.each(options, function(val) {
    return ($("<tr/>")
    .append(
        $("<td/>").html("4/6/2013 10:41") + 
        $("<td/>").html("5/6/2013 10:42") +
        $("<td/>").html(val) +
        $("<td/>").html("<img src='pdf_16x16.png'/>") +
        $("<td/>").html("<a href='#'>Re-Upload Documents</a>")
    ));
})



回答4:


try following:

$("<table/>",{"cellspacing":"0","cellpadding":"0","border":"0","width":"100%"})
.append("<tbody></tbody>").appendTo("body");

$("tbody").append(function(){
  options = ["ONE","TWO","THREE","FOUR"];
   var $container = $('<div></div>');

$.each(options, function(val) {
    $container.append($("<tr/>").append(
        $("<td/>").html("4/6/2013 10:41"),
        $("<td/>").html("5/6/2013 10:42"),
        $("<td/>").html(val),
        $("<td/>").html("<img src='pdf_16x16.png'/>"),
        $("<td/>").html("<a href='#'>Re-Upload Documents</a>")
    ));
});

  return $container.html();
});

working fiddle here: http://jsfiddle.net/mCLsm/2/



来源:https://stackoverflow.com/questions/18101099/for-loop-inside-the-append-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!