将焦点切换到单击以动态添加文本输入
让我先说一下我是一个 jQuery 菜鸟——刚刚开始学习过程。
我目前在一家网上商店工作。我们与购物车集成的功能之一是“快速添加”字段 (id="cc")。
“快速添加”字段下方是一个动态填充项目行的表。在每个动态添加的商品行中都有一个“数量”文本字段 (id="qtycart"),以防客户在将商品添加到购物车后想要更改其数量。
这就是我想要做的:
我希望客户能够将文本添加到“快速添加”文本输入,然后单击提交按钮。单击提交按钮后,我希望焦点从此输入更改为下面新添加的行上的“数量”输入。
这有道理吗?
这是我整理的一个简单函数,但我显然遗漏了一些东西:
$("#cc").live("click", function(){
$("#qtycart:text:visible:last").focus();
});
非常感谢您的时间!
Let me preface this question by saying that I'm a jQuery noob - just beginning the learning process.
I'm currently working on an online store. One of the features that we've integrated with the shopping cart is a "quick add" field (id="cc").
Below the "quick add" field is a table that is dynamically populated with item rows. In each of these dynamically-added item rows is a "qty" text field (id="qtycart") in case the customer wants to change their qty once the item is added to the cart.
Here's what I want to do:
I want a customer to be able to add text to the "quick add" text input and click the submit button. Once the submit button is clicked, I want the focus to change from this input to the "qty" input on the newly-added row below.
Does this make sense?
Here is a simple function I put together, but I'm obviously missing something:
$("#cc").live("click", function(){
$("#qtycart:text:visible:last").focus();
});
Thanks very much for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 DOM (qtycart) 中似乎有重复的 ID。您可能应该使用类名“qtycart”:
如果您定位的是正确的元素,则
.focus()
方法确实有效。这是一个小演示,可以模拟您想要执行的操作: http://jsfiddle.net/6VZf3/2/以下是 W3C 关于 ID 属性 的规定:
You seem to have duplicate IDs in your DOM (qtycart). You should probably use the class name 'qtycart' instead:
The
.focus()
method does work if you're targeting the right element. Here's a little demo to simulate what you're trying to do: http://jsfiddle.net/6VZf3/2/And here's what the W3C says about the ID attribute:
我想我理解你的问题。看一下这个例子:
http://jsbin.com/ahusi4/2/edit
可以输入新的商品,数量输入框获得焦点。
希望这有帮助。
鲍勃
I think I am understanding your question. Take a look at this example:
http://jsbin.com/ahusi4/2/edit
You can enter a new item and the qty input box gets the focus.
Hope this helps.
Bob