如何使用jQuery设置输入框的默认值? [英] How do I set the default value of an input box using jQuery?

查看:293
本文介绍了如何使用jQuery设置输入框的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改输入框的默认值,以便在重置表单时保留该值.

I want to change the default value of an input box such that when I reset the form, the value remains.

我有以下代码使用jQuery设置值,但是当按下reset键时,该值将成为初始值.

I have the following code that sets a value using jQuery but as reset is pressed, the value becomes the initial one.

<form>
Email: <input type="text" id="email" value="old value" />
<input type="reset" value="reset" />
</form>

$(function(){
    $("#email").val("New value");
});

推荐答案

Reset清除形式的所有值(默认行为).如果您希望这些值再次可见,则需要再次设置它们.因此,您需要在重置按钮的onclick上调用方法,然后再次填写表单.

Reset clears all values of the form (default behaviour). If you want the values to be visible again, you need to set them again. So you need to call a method onclick of the reset button and fill the form again.

这篇关于如何使用jQuery设置输入框的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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