jQuery 使用 Javascript 动态更改 HTML5 输入元素的“占位符”属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3919369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:20:55  来源:igfitidea点击:

Changing the "placeholder" attribute of HTML5 input elements dynamically using Javascript

jqueryhtmlplaceholder

提问by cemregr

I'm trying to dynamically update the HTML5 placeholderattribute of a text field using jQuery.

我正在尝试placeholder使用 jQuery动态更新文本字段的 HTML5属性。

$("textarea").attr("placeholder", "New placeholder text");

From Firebug, I can observe that the placeholderattribute is indeed changing. But in the rendered textareaelement, it stays the same. Any suggestions?

从 Firebug 中,我可以观察到该placeholder属性确实在发生变化。但在渲染textarea元素中,它保持不变。有什么建议?

采纳答案by Minkiele

If you are using Firebug I can assume you are using Firefox, and Firefox doesn't yet support placeholderattribute in the input fields itself.

如果您使用的是 Firebug,我可以假设您使用的是 Firefox,而 Firefox 尚不支持placeholder输入字段本身的属性。

Placeholder feature detection

占位符特征检测

I just tried on Chrome for Mac and it supports placeholder text on textareas (and changes via javascript)

我刚刚在 Mac 版 Chrome 上尝试过,它支持 textareas 上的占位符文本(并通过 javascript 进行更改)

2015 Update: sometimes I gain reputation for this answer, so I want to clarify that this was accepted as correct because at the time Firefox 3.6 didn't support placeholder attribute. Release 4 then added support ("fixed the issue" wouldn't have been fair) so the code of OP works as expected since then.

2015 年更新:有时我因这个答案而声名鹊起,所以我想澄清一下,这被认为是正确的,因为当时 Firefox 3.6 不支持占位符属性。然后第 4 版添加了支持(“修复了问题”是不公平的),因此 OP 的代码从那时起就按预期工作。

回答by tilak

try this :

尝试这个 :

$('#inputTextID').attr("placeholder","placeholder text");

回答by xavok

I think you have to do that :

我认为你必须这样做:

$("textarea").val('');
$("textarea").attr("placeholder", "New placeholder text");

回答by NomanJaved

<label for="myname">Name *</label>
<input type="text" name="myname" id="myname" title="Please enter your name"
       pattern="[A-Za-z ]+, [A-Za-z ]+"
       autofocus required placeholder="Last, First" />

Then you want to select the placeholder and replace that text with the new text that you want to enter.

然后您要选择占位符并将该文本替换为new text that you want to enter.

$("input[placeholder]").attr("placeholder", "This is new text");

This will replace the text of Last, Firstto This is new text.

这将替换Last, Firstto的文本This is new text

回答by overflow

working example of dynamic placeholder using Javascript and Jquery http://jsfiddle.net/ogk2L14n/1/

使用 Javascript 和 Jquery http://jsfiddle.net/ogk2L14n/1/的动态占位符的工作示例

<input type="text" id="textbox">
 <select id="selection" onchange="changeplh()">
     <option>one</option>
     <option>two</option>
     <option>three</option>
    </select>

function changeplh(){
    debugger;
 var sel = document.getElementById("selection");
    var textbx = document.getElementById("textbox");
    var indexe = sel.selectedIndex;

    if(indexe == 0) { 
     $("#textbox").attr("placeholder", "age");

}
       if(indexe == 1) { 
     $("#textbox").attr("placeholder", "name");
}
}

回答by Yogesh

HTML:

HTML:

<input type="text" placeholder="entername" id="fname"/>

JS:

JS:

$("#fname").attr("placeholder", "New placeholder text");