JQuery 将文本框集合设置为空值
我如何清空文本框的值,这是我已经解决的代码,但似乎不起作用
var txtName = $("input#txtName"),txtPrice = $("input#txtPrice");
第一种方法
$(txtName,txtPrice).val("");
- 这实际上是错误的,因为价格文本框现在将成为我想在其中搜索的上下文。
第二种方法
$([txtName,txtPrice]).val("");
- 我不明白为什么我应该这样做,因为它们已经是 jQuery 对象(但有效)
我将它们放入变量中,因为它们将在脚本中进一步使用。
How do i go about emptying the values of textboxes here is below code i h've worked out but doesn't seem to work
var txtName = $("input#txtName"),txtPrice = $("input#txtPrice");
First Method
$(txtName,txtPrice).val("");
- this is actually wrong because the price textbox would now become the context to search within i suppose.
Second Method
$([txtName,txtPrice]).val("");
- I don't understand why i should do this as they are already jQuery Objects(But works)
I Put them in variables as these are used further in the script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这里给定你的变量:
你可以直接访问 jQuery 方法,如下所示:
因为它们已经是 jQuery 对象。无需再次尝试将它们包装在
$()
jQuery 函数中。据我所知, jQuery 函数 接受数组的唯一方法是当数组元素为DOM 元素,而不是 jQuery 对象。 (如果它适用于似乎没有在 doco 中涵盖的 jQuery 对象,当我尝试它时,它对我不起作用。)因此,假设您知道每个 jQuery 对象只有一个元素(因为您在ID上选择),你可以尝试这个:
但这仍然会创建一个新的jQuery对象来保存这两个元素,所以当你可以按照我第一次提到的方式来做时,这似乎有点多余。
(另请注意,在您的选择器“input#txtName”中,“输入”部分是多余的,因为您随后通过 id 选择,并且 id 在页面上是(或应该是)唯一的。)
编辑:如果您有一个非常长的变量列表并想节省打字另一种方法是这样的:
Given your variables here:
You can directly access jQuery methods like so:
Because they are already jQuery objects. There is no need to try to wrap them in the
$()
jQuery function again.As far as I can see, the only way the jQuery function accepts an array is when the array elements are DOM elements, not jQuery objects. (If it works with jQuery objects that doesn't seem to be covered in the doco, and when I tried it it didn't work for me.) So given that you know each of your jQuery objects only has one element (because you selected on ID), you could try this:
But that is still going to create a new jQuery object that holds both elements, so it seems a bit redundant when you can just do it the way I first mentioned.
(Note also that in your selector "input#txtName" the "input" part is redundant since you then select by id and id is (or should be) unique on the page.)
EDIT: if you had a really long list of variables and wanted to save typing another way to do it would be like this:
令人惊讶的是这有效,
$([txtName,txtPrice]).each(function(){this.val("")});
Surprising that this worked,
$([txtName,txtPrice]).each(function(){this.val("")});
这里有几种方法可以做到这一点;
(顺便说一句,您的
txtPrice
输入中有一个$
符号。)第一种方法不起作用,因为它是使用 jQuery 选择器的一种方法。当您使用 jQuery 时,第一个参数将是选择器,第二个参数将是选择器工作的容器对象。基本上,这几乎是同样的事情;
由于
txtPrice
中没有txtName
,因此两个值都不会被清空。第二种方法之所以有效,是因为您将参数作为数组提供给 jQuery 选择器。它接受它并对数组的每个元素执行
.val()
操作。这种方式是合法的,但因为你的变量已经是 jQuery 对象,所以不需要使用它。Here is a few ways to do it;
(There is a
$
sign in yourtxtPrice
input by the way.)First Method didn't work because it's a way of using jQuery selector. When you use jQuery like that first parameter will be the selector and second will be the container object where the selector works. Basically it's almost same thing like this;
Because there is no
txtName
intxtPrice
neither value will be emptied.Second Method works because you're giving your parameters as an array to jQuery selector. It accepts it and does
.val()
action to every element of array. This way is legit but because your variables already jQuery objects there is no need to use this.或者
or