90

入力要素のプレースホルダー テキストを変更するにはどうすればよいですか?

たとえば、テキスト型の 3 つの入力があります。

<input type="text" name="Email" placeholder="Some Text">
<input type="text" name="First Name" placeholder="Some Text">
<input type="text" name="Last Name"placeholder="Some Text">

JavaScript または jQuery を使用してSome Textテキストを変更するにはどうすればよいですか?

4

8 に答える 8

164

Javascriptを使用したい場合は、getElementsByName()メソッドを使用してフィールドを選択し、input各フィールドを変更できplaceholderます...以下のコードを参照してください...

document.getElementsByName('Email')[0].placeholder='new text for email';
document.getElementsByName('First Name')[0].placeholder='new text for fname';
document.getElementsByName('Last Name')[0].placeholder='new text for lname';

それ以外の場合はjQueryを使用します。

$('input:text').attr('placeholder','Some New Text');
于 2012-11-22T06:03:05.530 に答える
29

このソリューションはjQueryを使用します。すべてのテキスト入力に同じプレースホルダーテキストを使用する場合は、次を使用できます。

$('input:text').attr('placeholder','Some New Text');

また、別のプレースホルダーが必要な場合は、要素のIDを使用してプレースホルダーを変更できます

$('#element1_id').attr('placeholder','Some New Text 1');
$('#element2_id').attr('placeholder','Some New Text 2');
于 2012-11-22T05:47:32.497 に答える
13
var input = document.getElementById ("IdofInput");
input.placeholder = "No need to fill this field";

詳細はplaceholderこちら: http://help.dottoro.com/ljgugboo.php

于 2015-12-21T17:22:55.283 に答える
5

jquery を使用すると、次のコードでこれを行うことができます。

<input type="text" id="tbxEmail" name="Email" placeholder="Some Text"/>

$('#tbxEmail').attr('placeholder','Some New Text');
于 2012-11-22T05:44:07.143 に答える
4

私は同じ問題に直面しています。

JS では、まずテキスト入力のテキスト ボックスをクリアする必要があります。そうしないと、プレースホルダー テキストが表示されません。

これが私の解決策です。

document.getElementsByName("email")[0].value="";
document.getElementsByName("email")[0].placeholder="your message";
于 2015-09-10T09:59:13.557 に答える