jQuery 选择器:当我有多个选择菜单时,如何指定一个选择菜单?
我有 4 个精选菜单,全部都是相同的产品。 (用户应该使用选择菜单将产品添加到发票中)。
因此,每个部分都由一个选择菜单、一个数量文本字段和一个价格文本字段组成。但我在同一页上有四个。
每当我从选择菜单中选择产品时,我想更改数量和价格。但更具体地说,我只想知道如何找出单击了哪个选择菜单。
如果选择菜单有一类产品 (.product),当我选择产品时,所有部分都会受到影响。但我只想影响特定的选择菜单部分。
$(".product").change(function(event){
alert('product picked'); // testing
});
我不能只附加一个数字,如下所示:product1、product2、product3。因为然后在 javascript 文件中我必须编写 4 个不同的函数,
$(".product1").change(function(event){,
$(".product2").change(function(event){, etc.
我知道这是非常基本的,但我需要刷新我的 jQuery 内容。
这是 HTML 的一些形式。为了简化起见,我仅包含产品选择菜单和数量文本字段。
<div class="item">
<p>
Product:
<select class="product" id="invoice_line_items_attributes_0_item_id" name="invoice[line_items_attributes][0][item_id]"><option value="1" data-defaultquantity="1">WP setup</option>
<option value="2" data-defaultquantity="1">WordPress Theme Design</option>
<option value="3" data-defaultquantity="1">WHE/yr</option>
<option value="4" data-defaultquantity="1">WHE/mo</option></select>
</p>
Qty: <input class="quantity" id="invoice_line_items_attributes_0_quantity" name="invoice[line_items_attributes][0][quantity]" size="30" type="text" value="1"><br>
</div><hr>
<div class="item">
<p>
Product:
<select class="product" id="invoice_line_items_attributes_1_item_id" name="invoice[line_items_attributes][1][item_id]"><option value="1" data-defaultquantity="1">WP setup</option>
<option value="2" data-defaultquantity="1">WordPress Theme Design</option>
<option value="3" data-defaultquantity="1">WHE/yr</option>
<option value="4" data-defaultquantity="1">WHE/mo</option></select>
</p>
Qty: <input class="quantity" id="invoice_line_items_attributes_1_quantity" name="invoice[line_items_attributes][1][quantity]" size="30" type="text" value="1"><br>
</div><hr>
I have 4 select menus, all with the same products. (User is supposed to use the select menus to add products to an invoice).
So each section is composed of a select menu, a quantity text field and a price text fields. But I have FOUR of those in the same page.
Whenever I select a product from the select menu, I want to change the quantity and price. But more specifically, I would just like to know, how to find out WHICH select menu was clicked.
if the select menus have a class of product (.product), when I select a product, ALL sections are affected. But I only want to affect that specific select menu section.
$(".product").change(function(event){
alert('product picked'); // testing
});
I can't just append a number, like this: product1, product2, product3. Because then in the javascript file i would have to write 4 different functions
$(".product1").change(function(event){,
$(".product2").change(function(event){, etc.
I know this is very basic, but I need to refresh my jQuery stuff.
This is some of the form HTML. I only included product select menu and quantity text field for simplification.
<div class="item">
<p>
Product:
<select class="product" id="invoice_line_items_attributes_0_item_id" name="invoice[line_items_attributes][0][item_id]"><option value="1" data-defaultquantity="1">WP setup</option>
<option value="2" data-defaultquantity="1">WordPress Theme Design</option>
<option value="3" data-defaultquantity="1">WHE/yr</option>
<option value="4" data-defaultquantity="1">WHE/mo</option></select>
</p>
Qty: <input class="quantity" id="invoice_line_items_attributes_0_quantity" name="invoice[line_items_attributes][0][quantity]" size="30" type="text" value="1"><br>
</div><hr>
<div class="item">
<p>
Product:
<select class="product" id="invoice_line_items_attributes_1_item_id" name="invoice[line_items_attributes][1][item_id]"><option value="1" data-defaultquantity="1">WP setup</option>
<option value="2" data-defaultquantity="1">WordPress Theme Design</option>
<option value="3" data-defaultquantity="1">WHE/yr</option>
<option value="4" data-defaultquantity="1">WHE/mo</option></select>
</p>
Qty: <input class="quantity" id="invoice_line_items_attributes_1_quantity" name="invoice[line_items_attributes][1][quantity]" size="30" type="text" value="1"><br>
</div><hr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的事件处理程序中,
this
将绑定到相关的一个方便的技巧是使用“数据”属性将
然后在处理程序中:
In your event handler,
this
will be bound to the relevant<select>
element.A handy trick is to use "data" attributes to relate something like that
<select>
to the other fields. That is, you can do something like store the "id" of the related field in a data attribute so that the handler can find the field to mess with:Then in the handler: