简体   繁体   English

如何删除特定的div标签并使用javascript重置其内容

[英]How to remove a particular div tag and reset its content using javascript

Code below contains certain tags in all four. 下面的代码包含所有四个中的某些标记。 Image-1 图像-1 在此输入图像描述 here is the code : 这是代码:

 <div style='background-color:YellowGreen;height:20px;width:100%;margin-top:15px;font-weight: bold;'>
            &nbsp;&nbsp;Delegate(s) details: </div>
            <div style="border:1px solid black;"><br/>
            <div id="delegates">
            <div id="0">
            &nbsp;&nbsp; Name of the Delegate: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input name='contact_person[]' type='text' size="50" maxlength="50" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            Designation:
             <select name='delegate_type_name[]' class='delegate_type'>
             <option value='select'>Select</option>
             <option value='Main'>Main</option>
             </select>
            </div><br/>
            </div>
            <div>
            &nbsp;&nbsp;
<input type="button" name="more" value="Add More Delegates" id="add_more" />
            <br />
            <br />
            </div>
            </div>

In the above code on line 5 where <div id="0"> changes to value 1 in script that I mentioned in "add_more" 在第5行的上面代码中, <div id="0">在我在“add_more”中提到的脚本中更改为值1

And the javascript for "add_more" is given below 并且下面给出了“add_more”的javascript

jQuery('#add_more').click(function(){
        var id = jQuery('#delegates > div:last').attr('id');
        var temp = "<div id='"+(parseInt(id)+parseInt('1'))+"'>&nbsp;&nbsp; Name of the Delegate: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' size='50' maxlength='50' name='contact_person[]' />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Designation:";
        temp += "<select name='delegate_type_name[]' class='delegate_type additional_delegate'><option value='select'>Select</option><option value='Additional'>Additional</option><option value='Spouse'>Spouse</option></select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' name='rem' value='Remove' id='remove' /></div><br/>";
        jQuery('#delegates').append(temp);
    });

In the javascript code above I have added a remove button in the temp+ variable 在上面的javascript代码中,我在temp +变量中添加了一个删除按钮

<input type='button' name='rem' value='Remove' id='remove' />

Image-2 shows the remove button every time I click on "Add more Delegates" button. 每次单击“添加更多代理”按钮时,图像2都会显示删除按钮。 在此输入图像描述 In the image-2 I click on Add More Delegates button it shows the "remove" button on the right of drop down select list. 在图像-2中,我单击“添加更多代理”按钮,它会在下拉选择列表右侧显示“删除”按钮。

I want a jQuery function for remove button, so that when I click on remove it should remove <div id="1"> and also reset content before removing the div tag. 我想要一个用于删除按钮的jQuery函数,这样当我点击删除它时应删除<div id="1">并在删除div标签之前重置内容。 Below image-3 is the output that I want when I click on remove button. 下面的图像-3是我点击删除按钮时想要的输出。 在此输入图像描述

code that I tried was this from some reference is this 我试过的代码就是这个来自于一些参考

jQuery('#remove').click(function(){
        var id = jQuery('#delegates > div:last').attr('id').remove();
    });

but no luck. 但没有运气。

Thanks. 谢谢。

你不能给一个只是数字的元素id,它必须是#mydiv1,#mydiv2或类似的东西,即以字母而不是数字开头。

For starters your markup is a total mess. 对于初学者来说,你的标记是一团糟。 There is no way you should be using &nbsp; 您无法使用&nbsp; for layout purposes. 用于布局目的。 Read up on tableless layouts and css. 阅读无表格布局和CSS。

The first thing you need to change is the id's of your div. 需要改变的第一件事是你的div的id。 An id cannot start with a numeric. id不能以数字开头。 I suggest naming the first div delegate0 . 我建议命名第一个div delegate0 Secondly, you are adding a remove button on every new row with the same id - all id's on a page should be unique so i suggest you change this to class="remove" . 其次,您在每个具有相同id新行上添加删除按钮 - 页面上的所有ID都应该是唯一的,因此我建议您将其更改为class="remove"

As for your question, it really boils down to needing to add a jQuery handler to the remove buttons using the .live docs method. 至于你的问题,它真的归结为需要使用.live docs方法向删除按钮添加jQuery处理程序。

This is as simple as: 这很简单:

jQuery('.remove').live('click',function(){
   $(this).closest('div').remove();
});

Also, you need to keep a running counter of the id of the items added, and increment this every time a new row is added. 此外,您需要保留已添加项目的ID的运行计数器,并在每次添加新行时递增此值。

var nextDelegate = 1;
jQuery('#add_more').click(function(){
   ... your code here
   nextDelegate++;
});

Also, I removed the superfluous <br/> after each div. 此外,我除去多余的<br/>每个格之后。

Live example: http://jsfiddle.net/cb4xQ/ 实例: http//jsfiddle.net/cb4xQ/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM