[英]Remove dropdown and add a textbox dynamically inside a <td> tag using jQuery
I have a dropdown inside a <td>
tag in a table row. 我在表格行的
<td>
标记内有一个下拉菜单。 I need to change it to a textbox or a textbox+button dynamically depending on some logic. 我需要根据某些逻辑将其动态更改为文本框或文本框+按钮。 Any help on how I can achieve this?
我如何实现此目标有帮助吗?
The whole table containing the <td>
's and <tr>
's along with the controls were generated dynamically. 包含
<td>
和<tr>
以及控件的整个表是动态生成的。
Thanks in advance. 提前致谢。
Take a look at replaceWith
jQuery method which replaces each element in the set of matched elements with the provided new content. 看一看
replaceWith
jQuery方法,它用提供的新内容替换匹配元素集中的每个元素。
Something like this will help you. 这样的事情会帮助您。
$('table td select').replaceWith('<input type="text" />');
I'm guessing since this is a <table>
you have multiple <td>
's and you're asking how to change the html for that particular <td>
with that dropdown box in it? 我猜是因为这是一个
<table>
您有多个<td>
,并且您在问如何使用其中的下拉框更改特定<td>
的html? There are many ways to select that element in jQuery but I think you will have the most luck simply specifying an id for that <td>
. 有很多方法可以在jQuery中选择该元素,但我认为,为该
<td>
指定一个ID会很幸运。 Since you're creating this dynamically, unique id for that <td>
is the easiest way to later refer to the element so you can reset the html in that element. 由于您是动态创建的,因此
<td>
唯一ID是稍后引用该元素的最简单方法,因此您可以重置该元素中的html。 If you have multiple <td>
with dropdowns, you could even insert an id (eg 45 in the case below) so you can change just that <td>
. 如果您有多个带有下拉菜单的
<td>
,甚至可以插入一个id(例如,在下面的情况下为45),以便可以仅更改<td>
。
$('#tdwithdropdown_45').html('<input ...>');
You could put a <div id="dynamic"></div>
tag inside the <td>
and you could use something like: 您可以在
<td>
内放置<div id="dynamic"></div>
标记,并可以使用类似以下内容的代码:
document.getElementById("dynamic").innerHTML="";
and then: 接着:
document.getElementById("dynamic").innerHTML="<input type='text' />";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.