带有输入值的内部HTML [英] Inner HTML with input values

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

问题描述

有一个关于innerHTML和输入值的简短问题。请参阅下面的简要示例(为方便起见使用jQuery):

have a brief question regarding innerHTML and input values that have been entered. See the brief example below (using jQuery for convenience):

http://jsfiddle.net/F7urT/2/

jQuery:

jQuery(document).ready(function($) {
    $('.send').click(function() {
        alert( $('.content').html() );
        return false;
    });
});​

html:

<div class="content">
    <input type="text" name="input" value="Old Value" />
    <input type="button" class="send" value="Send" />
</div>​

如果编辑输入值,请点击'发送'按钮,警报显示innerHTML gotten包含带有旧值的输入,而不是用户输入的值。为什么是这样?我们如何将HTML作为用户输入的输入值的字符串?

If you edit the input value, then click the 'Send' button, the alert shows that the innerHTML gotten contains the input with the "Old Value", rather than the value the user has entered. Why is this? And how can we get the HTML as a string with user entered input values?

推荐答案

新值存储为属性而不是一个属性,该值可以通过 inputelement.value 获得,修改该值不会影响该属性。如果你想要带有新值的html,只需将属性设置为新值。

The new value is stored as a property not an attribute, the value can be obtained by inputelement.value, modifying the value does not affect the attribute. If you want the html with the new value just set the attribute to the new value.

对于复选框和单选按钮设置checked属性,设置文本区域的innerHTML ,对于选择设置选项上的选定属性

For check boxes and radio buttons set the checked attribute, set the innerHTML for text areas, for selects set the selected attribute on the option

http://jsfiddle.net/mowglisanu/F7urT/5/

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

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