如何使用Jquery清除html输入文本字段的内容

时间:2022-11-13 19:09:01

I am having a html text field which can hide.

我有一个可以隐藏的html文本字段。

When hiding I want clear the text field.

隐藏时,我要清除文本字段。

I used this and it doesn't work.

我用过这个,但没用。

$('#id').val("");

$(' # id ').val(" ");

And I tried $('#id').text(""); as well.

我试过$(' # id ')。text(" ");。

It removed the whole text field.

它删除了整个文本字段。

4 个解决方案

#1


8  

This is correct:

这是正确的:

$('#id').val('');

However, your part 2 is interesting.

然而,你的第二部分很有趣。

$('#id').text("");

That shouldn't create the behavior you're describing. I expect that you're referencing the parent object, not the <input> directly.

这不应该产生你所描述的行为。我希望您正在引用父对象,而不是直接引用

#2


2  

What else is happening with your code... because this works for me...

你的代码还发生了什么……因为这对我有用……

<input id="id">
<input id="clear" type="button" value="Clear">

and

$('#clear').click(function(){
    $('#id').val('');    
});

Working example: http://jsfiddle.net/jasongennaro/xrxU8/

工作示例:http://jsfiddle.net/jasongennaro/xrxU8/

#3


1  

Assuming that TheTextField is the ID of your input field, this works fine.

假设这个textfield是你的输入字段的ID,这就没问题了。

$('#TheTextField').val("");
$('#TheTextField').hide();

Anyway, if you are using FireBug and you look at the input field while hidden, you will see that value attribute still has a value, which it really doesn't have, but when you show it again:

不管怎样,如果你在使用FireBug,你在隐藏的时候查看输入字段,你会发现value属性仍然有一个值,它实际上没有这个值,但是当你再次显示它的时候:

$('#TheTextField').val("");
$('#TheTextField').hide();
$('#TheTextField').show();

the input field as you will see it rendered in the browser as opposed to what you are seeing in the FireBug, it has no value anymore.

在浏览器中显示的输入字段与在FireBug中看到的相反,它不再具有任何值。

#4


1  

Please try this:

请试试这个:

 $('#id').attr("value","");

#1


8  

This is correct:

这是正确的:

$('#id').val('');

However, your part 2 is interesting.

然而,你的第二部分很有趣。

$('#id').text("");

That shouldn't create the behavior you're describing. I expect that you're referencing the parent object, not the <input> directly.

这不应该产生你所描述的行为。我希望您正在引用父对象,而不是直接引用

#2


2  

What else is happening with your code... because this works for me...

你的代码还发生了什么……因为这对我有用……

<input id="id">
<input id="clear" type="button" value="Clear">

and

$('#clear').click(function(){
    $('#id').val('');    
});

Working example: http://jsfiddle.net/jasongennaro/xrxU8/

工作示例:http://jsfiddle.net/jasongennaro/xrxU8/

#3


1  

Assuming that TheTextField is the ID of your input field, this works fine.

假设这个textfield是你的输入字段的ID,这就没问题了。

$('#TheTextField').val("");
$('#TheTextField').hide();

Anyway, if you are using FireBug and you look at the input field while hidden, you will see that value attribute still has a value, which it really doesn't have, but when you show it again:

不管怎样,如果你在使用FireBug,你在隐藏的时候查看输入字段,你会发现value属性仍然有一个值,它实际上没有这个值,但是当你再次显示它的时候:

$('#TheTextField').val("");
$('#TheTextField').hide();
$('#TheTextField').show();

the input field as you will see it rendered in the browser as opposed to what you are seeing in the FireBug, it has no value anymore.

在浏览器中显示的输入字段与在FireBug中看到的相反,它不再具有任何值。

#4


1  

Please try this:

请试试这个:

 $('#id').attr("value","");