JavaScriptを使用してHTML5入力要素の「プレースホルダー」属性を変更する
-
29-09-2019 - |
質問
HTML5を動的に更新しようとしています placeholder
jQueryを使用したテキストフィールドの属性。
$("textarea").attr("placeholder", "New placeholder text");
Firebugから、私はそれを観察することができます placeholder
属性は確かに変化しています。しかし、レンダリングされたもの textarea
要素、それは同じままです。助言がありますか?
解決
あなたがFirebugを使用している場合、私はあなたがFirefoxを使用していると仮定することができます、そしてFirefoxはまだサポートしていません placeholder
入力フィールド自体の属性。
Mac用Chromeを試したばかりで、Textareasのプレースホルダーテキストをサポートしています(JavaScriptによる変更)
2015年の更新:この回答の評判を得ることがあるので、Firefox 3.6がプレースホルダー属性をサポートしていなかったため、これが正しいと受け入れられたことを明確にしたいと思います。リリース4はサポートを追加しました(「問題を修正した問題」は公平ではなかったでしょう)。そのため、OPのコードはそれ以来予想どおりに機能します。
他のヒント
これを試して :
$('#inputTextID').attr("placeholder","placeholder text");
私はあなたがそれをしなければならないと思います:
$("textarea").val('');
$("textarea").attr("placeholder", "New placeholder text");
<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" />
次に、プレースホルダーを選択し、そのテキストを new text that you want to enter
.
$("input[placeholder]").attr("placeholder", "This is new text");
これは、のテキストを置き換えます Last, First
に This is new text
.
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");
}
}
HTML:
<input type="text" placeholder="entername" id="fname"/>
JS:
$("#fname").attr("placeholder", "New placeholder text");
所属していません StackOverflow