jQuery 选择 input[type=image] 兄弟姐妹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2206340/
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 select input[type=image] siblings
提问by The Disintegrator
I'm trying to convert these forms to ajax (an example)
我正在尝试将这些表单转换为 ajax(示例)
<table>
<tr>
<td>Prov</td><td>Cod</td><td>Nombre</td><td>Precio</td><td>Stock1</td><td></td><td></td>
</tr>
<tr class="o d">
<td class="provName">Karabitian</td>
<td class="cod d">494011135</td>
<td class="name" id="artID13899">Eje del. m505 y4ch98040 comp. s/cierre Shimano</td>
<td class="alignright">$ 69.00</td>
<td class="center">-</td>
<td class="add">
<nobr>
<form name="formularioAgregaItem" action="script.php" method="POST" autocomplete="off" class="add">
<fieldset>
<input name="function" value="cartAddByBarcode" type="hidden">
<input name="barcode" value="KAR005834" type="hidden">
<input name="orderID" value="23333" type="hidden">
<input name="qty" id="qty13899" size="2" maxlength="3" class="i" value="15" type="text">
<input src="add_16x16.gif" alt="agregar a la orden" type="image">
</fieldset>
</form>
</nobr>
</td>
</tr>
<tr class="o d">
<td class="provName">Karabitian</td>
<td class="cod d">494011137</td>
<td class="name" id="artID13900">Eje del. m565 y4c698010 comp. s/cierre Shimano</td>
<td class="alignright">$ 260.00</td>
<td class="center">-</td>
<td class="add">
<nobr>
<form name="formularioAgregaItem" action="script.php" method="POST" autocomplete="off" class="add">
<fieldset>
<input name="function" value="cartAddByBarcode" type="hidden">
<input name="barcode" value="KAR005835" type="hidden">
<input name="orderID" value="23333" type="hidden">
<input name="qty" id="qty13900" size="2" maxlength="3" class="i" value="15" type="text">
<input src="add_16x16.gif" alt="agregar a la orden" type="image">
</fieldset>
</form>
</nobr>
</td>
</tr>
<tr class="o d">
<td class="provName">Karabitian</td>
<td class="cod d">494011143</td>
<td class="name" id="artID13905">Eje del. m775 y26k98030 comp. s/cierre Shimano</td>
<td class="alignright">$ 290.00</td>
<td class="center">-</td>
<td class="add">
<nobr>
<form name="formularioAgregaItem" action="script.php" method="POST" autocomplete="off" class="add">
<fieldset>
<input name="function" value="cartAddByBarcode" type="hidden">
<input name="barcode" value="KAR005837" type="hidden">
<input name="orderID" value="23333" type="hidden">
<input name="qty" id="qty13905" size="2" maxlength="3" class="i" value="15" type="text">
<input src="add_16x16.gif" alt="agregar a la orden" type="image">
</fieldset>
</form>
</nobr>
</td>
</tr>
<tr class="o d">
<td class="provName">Karabitian</td>
<td class="cod d">494011153</td>
<td class="name" id="artID13910">Eje del. r500 y4bg98030 comp. s/cierre Shimano</td>
<td class="alignright">$ 73.00</td>
<td class="center">-</td>
<td class="add">
<nobr>
<form name="formularioAgregaItem" action="script.php" method="POST" autocomplete="off" class="add">
<fieldset>
<input name="function" value="cartAddByBarcode" type="hidden">
<input name="barcode" value="KAR005841" type="hidden">
<input name="orderID" value="23333" type="hidden">
<input name="qty" id="qty13910" size="2" maxlength="3" class="i" value="15" type="text">
<input src="add_16x16.gif" alt="agregar a la orden" type="image">
</fieldset>
</form>
</nobr>
</td>
</tr>
<tr class="o d">
<td class="provName">Karabitian</td>
<td class="cod d">494011157</td>
<td class="name" id="artID13914">Eje del. r560 y4c498010 comp. s/cierre Shimano</td>
<td class="alignright">$ 137.00</td>
<td class="center">-</td>
<td class="add">
<nobr>
<form name="formularioAgregaItem" action="script.php" method="POST" autocomplete="off" class="add">
<fieldset>
<input name="function" value="cartAddByBarcode" type="hidden">
<input name="barcode" value="KAR005845" type="hidden">
<input name="orderID" value="23333" type="hidden">
<input name="qty" id="qty13914" size="2" maxlength="3" class="i" value="15" type="text">
<input src="add_16x16.gif" alt="agregar a la orden" type="image">
</fieldset>
</form>
</nobr>
</td>
</tr>
</table>
But I cant figure out how to get the inputs from the same row i clicked
但我不知道如何从我单击的同一行获取输入
$('input[type=image]').click(function(){
// insert voodoo go get inputs here
// ajax call
return false;
});
I can't do something like $(input[name="barcode"]) Any ideas?
我不能做类似 $(input[name="barcode"]) 的事情有什么想法吗?
回答by Andreas Niedermair
you need to do sth like:
你需要做某事:
$('input[type=image]').click(function(){
var image = $(this);
var parent = image.parent(); // this will be the fieldset, could also do .closest('fieldset)', if the fieldset is not the immediate parent
var neededInput = $('input[name=barcode]', parent);
// do something with it
return false;
});
or using the siblings()
-method
或使用siblings()
- 方法
$('input[type=image]').click(function (event){
event.preventDefault();
var $image = $(this);
var $neededInput = $image.siblings('input[name=barcode]');
// do something with it
});
回答by dxh
inside your click function, this
will be a reference to the image you clicked. So you can do
在您的点击功能中,this
将是对您点击的图像的引用。所以你可以做
$(this).siblings()
or
或者
$(this).closest('fieldset').find(...);