$ .getJSON数据不在循环中显示ajaxSubmit

时间:2012-09-05 08:23:54

标签: php jquery

我尝试使用jquery执行动态加载的表单时遇到了一些问题。这是我的代码:

$(this).ajaxSubmit({ 
    url: '/postform1.php',
    type: 'post',
    //target: '#preview',
    success: function(msg) {

        var s = msg + '<form name="sr" id="sr" action="postform.php" method="post"><input type="text" /></form>';
        $('#preview').html(s);

        $.getJSON("/postform1.php", function(data){
        var counter = 0;
        if(data) {
           $.each(data, function(i, val) {
                var newTextBoxDiv = $(document.createElement('div')).attr("id", val);
                newTextBoxDiv.after().html('<label>' + i + ' : </label>' + '<input type="text" name="' + i + '" id="' + i + '" value="' + val + '" >');
                newTextBoxDiv.appendTo("#sr");
                counter++;
            });
        }
        });
    }
 });

我最初是从postform1.php回显整个表单,但发现表单没有提交。我不知道这是否是正确的方式,但我想我试一试。如果有人有任何想法,你会听到很高兴。

2 个答案:

答案 0 :(得分:0)

您错过了宣布:

contentType: " ... ",
dataType: " ... "

答案 1 :(得分:0)

感谢您对此进行调查,并设法对其进行排序。我也改变了这个:

var newTextBoxDiv = $(document.createElement('div')).attr("id", val);  
            newTextBoxDiv.after().html('<label>' + i + ' : </label>' + '<input type="text" name="' + i + '" id="' + i + '" value="' + val + '" >');  
            newTextBoxDiv.appendTo("#sr");  
            counter++;  

到此:

var newTextBoxDiv = $(document.createElement('div')).attr("id", val);
                newTextBoxDiv.html('<label>' + i + ' : </label>' + '<input type="text" name="' + i + '" id="' + i + '" value="' + val + '" >');
                newTextBoxDiv.appendTo(".sr");
                counter++;
相关问题