Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.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
Javascript 使用JQuery重新设置字段值_Javascript_Jquery - Fatal编程技术网

Javascript 使用JQuery重新设置字段值

Javascript 使用JQuery重新设置字段值,javascript,jquery,Javascript,Jquery,我有以下代码: $('#name1').focus(function() { if ($(this).val() == 'start value') { console.log($(this).val()); // this prints "start value" $(this).val() == '' } 我想做的是,当用户关注文本输入#name1时,该字段的值将消失。但是,上述操作不起任何作用,文本值保持不变。为什么会发生这种情况?我需要改

我有以下代码:

$('#name1').focus(function() {
    if ($(this).val() == 'start value') {
        console.log($(this).val()); // this prints "start value"
        $(this).val() == ''
    }
我想做的是,当用户关注文本输入
#name1
时,该字段的值将消失。但是,上述操作不起任何作用,文本值保持不变。为什么会发生这种情况?我需要改变什么

$('#name1').focus(function() {
    if ($(this).val() == 'start value') {
        console.log($(this).val()); // this prints "start value"
        $(this).val("");
    }
必须在括号中写入赋值,如
$(this).val(“”)

要指定值,请按如下方式使用:

$(this).val('');
this.val()
是一个函数,应该像调用
$(this.val('new value')
一样调用它来设置值。

而不是
$(this).val()=''
尝试
$(this.val(“”)

为什么不将html5占位符属性与Modernizer之类的旧浏览器支持一起使用,这样您就不用编写大量的功能,只需执行placeholder=“your text”即可实现相同的效果。

在我看来,您似乎在尝试创建占位符。。。如果是这样的话,为什么不使用HTML5
占位符
属性,以及现有的各种属性之一呢?