Saya terjebak di Masalah jQuery. saya pikir saya salah dalam kode ini. tolong bantu.

Saya mencoba mendapatkan nilai kotak input dengan mengklik tombol perbarui. tetapi setiap kali saya hanya mendapatkan nomor pertama.

Saya ingin nilai teks sesuai tombol pembaruan.

Terima kasih di Lanjutan

$(".update").click(function(){
	qty = $(".qty").val();	
	alert(qty);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
	<tr>
  		<td><input type="text" name="qty" value="5" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="20" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="30" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="50" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="45" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="15" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="70" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
</table>
1
Champ Decay 31 Agustus 2017, 13:27

2 jawaban

Jawaban Terbaik

Anda harus "menautkan" tombol pembaruan Anda dengan input ".qty" yang ingin Anda kaitkan

$(".update").click(function(){
	qty = $(this).closest("tr").find(".qty").val();	
	alert(qty);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
	<tr>
  		<td><input type="text" name="qty" value="5" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="20" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="30" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="50" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="45" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="15" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="70" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
</table>
4
Stéphane Ammar 31 Agustus 2017, 10:30

Ubah variabel qty seperti di bawah ini: var thisObj = $(this); qty = $(thisObj).parent().parent().find("input").val();

$(".update").click(function(){
	qty = $(".qty").val();	
	alert(qty);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
	<tr>
  		<td><input type="text" name="qty" value="5" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="20" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="30" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="50" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="45" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="15" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
	<tr>
  		<td><input type="text" name="qty" value="70" class="qty" id="qty"/></td>
		<td><button class="update">&#x21BB;</button></td>
  	</tr>
</table>
-1
user1904110 31 Agustus 2017, 10:34