Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除值=";评论;一旦从输入框中单击它_Php_Javascript_Html - Fatal编程技术网

Php 删除值=";评论;一旦从输入框中单击它

Php 删除值=";评论;一旦从输入框中单击它,php,javascript,html,Php,Javascript,Html,我创建了一个输入框,在用户输入任何内容之前说“评论” <input type="text" name="saysome" value = "comments?"/> 但是,我想在点击后立即删除这个“评论”。我正在尝试像这里的搜索框一样使用输入框,实际上完全相同。我该怎么做?只能由javascipt完成吗( 谢谢,不用jQuery: 给输入一个ID,并使用onclick事件清除其值 <input type="text" name="test" id="t

我创建了一个输入框,在用户输入任何内容之前说“评论”

          <input type="text" name="saysome" value = "comments?"/>

但是,我想在点击后立即删除这个“评论”。我正在尝试像这里的搜索框一样使用输入框,实际上完全相同。我该怎么做?只能由javascipt完成吗(

谢谢,不用jQuery:

给输入一个ID,并使用onclick事件清除其值

<input type="text" name="test" id="test" value="test" onclick="if(document.getElementById('test').value=='test')document.getElementById('test').value='';">


还支持不使用HTML 5的旧浏览器。

您可以使用此处的html5占位符属性:

例如:

 <input type="text" name="saysome" placeholder = "comments?"/>

对于不支持HTML5的浏览器,您也可以采用javascript方法。

非jquery:

onClick="clearComments()"

function clearComments() {
    commentInput = document.getElementById("commentsId");
    if(commentInput.value == 'comments?') {
       commentInput.value = '';
    }

}

正如其他评论者所提到的,您应该查看占位符。不过,要回答您的问题,如果用户尚未输入内容,此方法将在鼠标单击时删除文本。这假设输入的id为
textbox
。您必须将其更改为您拥有的任何内容,或者为输入分配一个id

<input id="textbox" type="text"/>

这个简单的方法可以在框有焦点时清除它,而不是在用户输入任何内容时

<input type="text" name="TB" value="Please Enter.." onfocus="this.value==this.defaultValue?this.value='':null"/>



查看此示例@

尝试
占位符=“
而不是
值=
,对于较旧的浏览器,使用a。不确定PHP是否适用于此处的任何内容-1,这将在每次清除它,而不仅仅是在第一次文本显示“评论”时很好。暂时修复了它。最好是使用一个单独的变量来防止它发生多次。。。
<input type="text" name="TB" value="Please Enter.." onfocus="this.value==this.defaultValue?this.value='':null"/>
<input type="text" name="saysome" onblur="if(this.value=='') this.value='comments?';" onclick="this.value=''" value="comments?" />