2

我在两页上有一个主 div,page1.phpform.php

page1.php

<div class="main_div">
    <?php require('form.php'); ?>
</div>

<script>
    $(".add_one").click(function(){
        $(".main_div").insertAfter(".single_div").load('form.php');
    });
</script>

表单.php

<div class="single_div">
    <table>
        <tr>
            <td><input type="text" class="text1" /></td>
            <td><input type="button" class="add_one" value="Add" /></td>
            <td>Some other fields too</td>
        </tr>
    </table>
</div>

css.css

.single_div {
    background-color: green;
    float: left;
    margin-left: 10px;
}

现在,当我单击按钮时,它不会在它旁边Add添加其他form/ ...但它消失了div class='single_class'

当我检查时inspect elementChrome Developer Tool什么都没有......除了javascript之外,整个身体都是空的

现在我如何load在单击按钮旁边添加页面选项。

编辑/添加

http://jsfiddle.net/marafee1243/ErN5E/

上面的小提琴可能不合适......

4

2 回答 2

1

What happens in your code with insertAfter is this; it takes the element, removes it from the DOM tree, then tries to insert it after the target. By the time it tries to insert it after the target, the target is removed from the DOM tree. Hence it's empty.

Try the following instead:

$(".main_div").clone(true).insertAfter(".single_div").load('www.google.com');

Including true in the clone() makes sure events are copied as well.

Sadly, there seems to be a lot more wrong here. I'm not quite sure what you are trying to do. You just want to have a copy of that single_class div after each click on the add button?

于 2012-06-27T11:23:39.353 回答
1
$(".main_div").insertAfter(".single_div").load('form.php');

在 .single_div 之后插入 .main_div,这没有意义

.insertAfter(target)
目标:选择器、元素、HTML 字符串或 jQuery 对象;匹配的元素集将插入到此参数指定的元素之后。

我想你想要这样的东西:

$('.single_div').eq(0).clone(true).appendTo('.main_div');
于 2012-06-27T11:18:39.590 回答