Div 自动生成时自动关闭

Div automatically closing as generated

本文关键字:自动生成 Div      更新时间:2023-09-26

我的div生成有问题,因为我生成了一个表单,我想将它们包装在具有相同ID的div中。但是当我检查元素时,它会在表单之前自动关闭。所以它不会像上面那样环绕它,它只是<div></div>在形式之上。

var startDiv = "<div id='appm'>";
var endDiv = "</div>";
for(var i = 0; i < values.length; i = i + 8){   
    $('#youEvents').append(     
        $('<form />', { id: values[i], method: 'POST' }).append(
            startDiv, // Starting div id=appm
            $('<textarea />', { id: "teast", name: 'routename', placeholder: 'Name', value: values[i], type: 'text' }),
            $('<br />'),
            $('<input />', { id: 'rname', name: 'routename', placeholder: 'Name', value: values[i + 1], type: 'text' }),
            $('<br />'),
            $('<input />', { id: 'rname', name: 'routename', placeholder: 'Name', value: values[i + 2], type: 'text' }),
            $('<br />'),
            $('<input />', { id: 'rname', name: "ee", placeholder: 'Name', value: values[i + 3], type: 'text' }),
            $('<br />'),
            $('<input />', { id: 'rname', name: 'routename', placeholder1: 'Name', value: values[i + 4], type: 'text' }),
            $('<br />'),
            $('<input />', { id: 'rname', name: 'routename', placeholder: 'Name', value: values[i + 5], type: 'text' }),
            $('<br />'),
            $('<input />', { id: 'address', id: 'rdescription', name: 'heya', value: values[i + 6], type: 'text' }),
            $('<br />'),
            $('<input />', { id: 'adress', name: 'routetags', placeholder: 'tags', value: values[i + 7], type: 'text' }),
            $('<br />'),                                    
            $('<input />', { id: values[i], type: 'button', value: 'Submit',  click: function(){
                // attaching the function to the button
                testAjax(this.id);  // Calling the function below.                                                  
            }}),
            endDiv // Ending the div         
        )
    );
}

这是因为jQuery在追加 <div id='appm'>div创建了 HTML 元素,其中包括结束标记。因此,符号$('<form />') ,例如,结果与$('<form>')相同。

为了实现你想要的,你可以通过使用另一个嵌套.append来做你已经在做的事情:

$('<form>', { id: values[i], method: 'POST' }).append(
    $('<div>', { id: 'appm' }).append(
        $('<textarea>', { id: "teast", name: 'routename', placeholder: 'Name', value: values[i], type: 'text' }),
        $('<br>'),
        // [...] 
        $('<br>'),                                    
        $('<input>', { id: values[i], type: 'button', value: 'Submit',  click: function(){ testAjax(this.id); } })
    )
)