jQuery在不删除其他元素的情况下将新元素插入表单元格 [英] jquery insert new element into table cell without erasing other elements

查看:70
本文介绍了jQuery在不删除其他元素的情况下将新元素插入表单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个表包含几行,每行有四个单元格.

A table contains several rows, each with four cells.

在具有id=tdJane的单元格中,我已经有两个输入元素:

In a cell with id=tdJane, I already have two input elements:

<table>
    <tr>
        <td id="tdBob">
            <input type="hidden" id="hida" value="some stuff">
            <input type="hidden" id="hidb" value="other stuff">
        <td>
        <td id="tdJane">
            <input type="hidden" id="hid1" value="some text">
            <input type="hidden" id="hid2" value="other text">
        <td>
    </tr>
</table>

在单元格#tdJane中,我希望在#hid2

Into cell #tdJane, I wish to insert a new hidden input field below #hid2

我尝试过:

$('#tdJane').html('<input type="hidden" id="hid3" value="newest text">') 

但这会覆盖单元格的现有内容.

but that overwrites the existing contents of the cell.

我该怎么办?

推荐答案

您需要使用 .append () .html()会覆盖其中的内容.

You need to use .append(), .html() will overwrite the contents.

$('#tdJane').append('<input type="hidden" id="hid3" value="newest text">');

您可以使用jquery元素构造函数来获得更清晰的显示.

You can use jquery element constructor for more clarity.

 $('#tdJane').append($('<input/>',{type:'hidden',
                                   id: 'hid3',
                                   value:'newest text'}));

在此 演示

See viewsource in this Demo

这篇关于jQuery在不删除其他元素的情况下将新元素插入表单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆