JQuery 将文本框集合设置为空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8652746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery set textbox collection to empty value
提问by Deeptechtons
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)
- 我不明白为什么我应该这样做,因为它们已经是 jQuery 对象(但有效)
I Put them in variables as these are used further in the script.
我将它们放在变量中,因为它们会在脚本中进一步使用。
回答by Emre Erkan
Here is a few ways to do it;
这里有几种方法可以做到;
txtName.add(txtPrice).val("");
// OR
$("input#txtName,input#txtPrice").val("");
(There is a $
sign in your txtPrice
input by the way.)
(顺便说一下$
,您的txtPrice
输入中有一个标志。)
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;
第一种方法不起作用,因为它是使用 jQuery 选择器的一种方式。当您使用 jQuery 时,第一个参数将是选择器,第二个将是选择器工作的容器对象。基本上是这样的;
$(txtPrice).find(txtName).val("");
Because there is no txtName
in txtPrice
neither value will be emptied.
因为txtName
在txtPrice
两者中都没有值会被清空。
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.
第二种方法有效,因为您将参数作为数组提供给 jQuery 选择器。它接受它并对.val()
数组的每个元素执行操作。这种方式是合法的,但是因为您的变量已经是 jQuery 对象,所以没有必要使用它。
回答by Jason Sebring
$('input[type=text]').val('');
or
或者
$('input[type=text]').each(function(){ $(this).val(''); });
回答by nnnnnn
Given your variables here:
鉴于您的变量在这里:
var txtName = $("input#txtName"),txtPrice = $("input#txtPrice");
You can directly access jQuery methods like so:
您可以像这样直接访问 jQuery 方法:
txtName.val("");
txtPrice.val("");
Because they are alreadyjQuery objects. There is no need to try to wrap them in the $()
jQuery function again.
因为它们已经是jQuery 对象了。无需再次尝试将它们包装在$()
jQuery 函数中。
As far as I can see, the only way the jQuery functionaccepts 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:
据我所知,jQuery 函数接受数组的唯一方法是数组元素是 DOM 元素,而不是 jQuery 对象。(如果它适用于 doco 中似乎没有涵盖的 jQuery 对象,当我尝试它时它对我不起作用。)因此,鉴于您知道每个 jQuery 对象只有一个元素(因为您在 ID 上选择),你可以试试这个:
$([txtName[0],txtPrice[0]]).val("");
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.
但这仍然会创建一个包含这两个元素的新 jQuery 对象,因此当您可以按照我最初提到的方式进行操作时,这似乎有点多余。
(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.)
(另请注意,在您的选择器“input#txtName”中,“input”部分是多余的,因为您随后按 id 选择,而 id 在页面上是(或应该是)唯一的。)
EDIT: if you had a really long list of variables and wanted to save typing another way to do it would be like this:
编辑:如果你有一个很长的变量列表,并想以另一种方式保存输入,它会是这样的:
$.each([txtName,txtPrice,txtTest1,txtTest2,txtTest3],function(i,v) { v.val(""); });
回答by Deeptechtons
Surprising that this worked,
$([txtName,txtPrice]).each(function(){this.val("")});
令人惊讶的是,这有效,
$([txtName,txtPrice]).each(function(){this.val("")});