改变HTML的输入值 [英] HTML- Altering Input Value

查看:84
本文介绍了改变HTML的输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发布了一个类似的问题,但经过一番讨论后,问题变得与最初陈述的有所不同,所以我认为最好将这个问题作为一个新问题发布。我试图使用输入字段来显示总数(我会考虑使用另一种元素类型,但是我需要在其他需要输入字段的地方执行相同的操作,所以我现在也可以修复这个问题) 。问题是包含这些字段的表的HTML需要重新加载,即在表创建时,我使用Jquery $('#table1')。html($('#table1' ).html()+< next_table_line>); 。这会导致输入字段恢复为其属性中显示的值,当使用 $('#input1' ).val(< some_value>); setter或手动输入数据。有谁知道我的问题的解决方案?我需要能够在不丢失数据的情况下向表中添加行,但我无法弄清楚如何执行此操作。 解决方案

($('#table1')。html()+< next_table_line>); C $ C>。您应该追加到您的表格。以下示例小表:

 < table id =test> 
< tr>< td>测试< / td>< / tr>
< / table>

现在,要添加一个新行,不要重写整个表格HTML,只需追加!

  $(#test)。append(< tr>< td>请参阅im new!< / td> ;< / TR>中); 

演示: http://jsfiddle.net/uB2Rq/1/


I recently posted a similar question, but after some discussion, the question turned out to be quite a bit different than it was originally stated, so I thought it best to post this as a new question. I am trying to use an input field to show a total (I would consider using another element type, but I need to do the same thing in other places where an input field is actually required, so I may as well fix the problem now). The problem is that the HTML of the table containing these fields needs to be reloaded on occasion, i.e. in table creation, I use the Jquery $('#table1').html($('#table1').html() + <next_table_line>);. This causes the input field to revert to the value displayed in its value attribute, which doesn't change when I use the $('#input1').val(<some_value>); setter or when I enter data manually. Does anyone know of a solution to my problem? I need to be able to add rows to the table without loosing my data, but I can't figure out how to do this.

解决方案

You're reloading your entire table with the line $('#table1').html($('#table1').html() + <next_table_line>);. You should be appending to your table instead. Take the following sample small table:

<table id="test">
    <tr><td>test</td></tr>
</table>

Now, to add a new row, dont re-write the entire table HTML, just append!

$("#test").append("<tr><td>See im new!</td></tr>");

Demo: http://jsfiddle.net/uB2Rq/1/

这篇关于改变HTML的输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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