"默认值"属性为< select>? [英] "defaultValue" property for <select>?

查看:86
本文介绍了"默认值"属性为< select>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript具有 textObject.defaultValue = somevalue ,用于检索输入的默认值(页面加载的存储值),即使您擦除输入并替换内容,您也可以仍然可以获得默认值。像这样:

Javascript has textObject.defaultValue=somevalue for retrieving the default value (stored value from page load) of an input even if you wipe the input and replace the contents, you can still get the default back. Like this:

// in the html page
<input id="addr1" type="text" value="21 Oak St." />

// the jquery
myInput = $("#addr1"); // the input, default value on page load = 21 Oak St.

$(myInput).val('51 New St'); // wipe default and set new

// alerts 21 Oak St
alert($(myInput).val($(myInput)[0].defaultValue));

如何在选择中完成此功能?

selectedIndex 是布尔值,不是值,所以不起作用。

selectedIndex is boolean, not the value, so that does not work.

谢谢!

推荐答案

您可能想查看option元素的defaultSelected属性。

You probably want to look at the "defaultSelected" attribute of "option" elements.

最初,如果选项标签的原始HTML具有明确的selected属性,那么defaultSelected将为true。您可以通过在页面加载后使用Javascript设置选项标签上的属性来更改此选项,以响应您喜欢的任何条件。

Initially, "defaultSelected" will be true if the original HTML of the option tag had an explicit "selected" attribute. You can change that by setting the attribute on option tags with Javascript after the page has loaded, in response to whatever conditions you like.

这篇关于&QUOT;默认值&QUOT;属性为&lt; select&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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