Dee*_*ons 4 jquery input jquery-selectors
我如何在这里清空文本框的值是在我已经解决的代码下面,但似乎不起作用
var txtName = $("input#txtName"),txtPrice = $("input#txtPrice");
Run Code Online (Sandbox Code Playgroud)
$(txtName,txtPrice).val("");
Run Code Online (Sandbox Code Playgroud)
$([txtName,txtPrice]).val("");
Run Code Online (Sandbox Code Playgroud)
我将它们放在变量中,因为这些变量在脚本中进一步使用.
Emr*_*kan 11
这有几种方法可以做到;
txtName.add(txtPrice).val("");
// OR
$("input#txtName,input#txtPrice").val("");
Run Code Online (Sandbox Code Playgroud)
(顺便说一句$,你的txtPrice输入中有一个标志.)
First Method不起作用,因为它是使用jQuery选择器的一种方式.当您使用jQuery时,第一个参数将是选择器,第二个参数将是选择器工作的容器对象.基本上这几乎是这样的;
$(txtPrice).find(txtName).val("");
Run Code Online (Sandbox Code Playgroud)
因为没有txtName在txtPrice既不值将被清空.
第二种方法有效,因为您将参数作为数组提供给jQuery选择器.它接受它并对.val()数组的每个元素执行操作.这种方式是合法的,但因为你的变量已经是jQuery对象,所以不需要使用它.
| 归档时间: |
|
| 查看次数: |
30549 次 |
| 最近记录: |