我有一些输入字段与select2,
具有相同的类选择类型和车库后,当我点击库存字段时,之前选择的值将被放在点击的库存上,但是我的代码给我的第一次和第二次相同,
我在jsfiddle的代码
http://jsfiddle.net/kabircse/5fh5jmxu/
你能帮帮我吗?
Html代码:
<div id="items">
<p>Type:</p>
<select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
<p>Garage:</p>
<select name="garage[]" class="select2 gd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<p>Stock:</p><input type="text" name="stock[]" class="stock">
<p>Type:</p>
<select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
<p>Garage:</p>
<select name="garage[]" class="select2 gd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<p>Stock:</p><input type="text" name="stock[]" class="stock">
<p>Type:</p>
<select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
<p>Garage:</p>
<select name="garage[]" class="select2 gd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<p>Stock:</p><input type="text" name="stock[]" class="stock">
<p>Type:</p>
<select name="Type2[]" class="select2 pd2" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
<p>Garage:</p>
<select name="garage2[]" class="select2 gd2" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
Stock<input type="text" name="stock2[]" class="stock2">
</div>
JS:
$(".select2").select2();
$('#items').on('click','.stock',function(){
var indx = $('.stock').index(this);
var p_id = $('.pd').eq(indx).select2("val");
var g_id = $('.gd').eq(indx).select2("val");
v = p_id+g_id;
$('.stock').eq(indx).val(v);
});
答案 0 :(得分:1)
尝试了解
的基础知识 var parentTr = $(this).closest('tr');
var type = $(parentTr).find('td:eq(0) select').val();
var garage = $(parentTr).find('td:eq(1) select').val();
nearest()向上移动DOM树而不是向下移动,所以如果$(this)等于当前行,你将永远不会使用最接近的(&#39; td&#39;)找到其中的td标签。
假设您不能直接使用id属性来访问输入,正如Jamie建议的那样,您需要使用:
$(this).children("td:eq(0)");
。 。 。获取行的第一个td,假设$(this)引用了您想要的行。要访问其中的select值,请使用:
$(this).children("td:eq(0)").find("select").val();
$(this).closest("tr").children("td:eq(0)").find("select").val();
。 。 。要么 。 。
$(this).closest("tr").children("td:eq(0) select").val();
(第一个更快)
答案 1 :(得分:0)
请找到以下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link rel="stylesheet" href="../repo/css/bootstrap.css" type="text/css" />
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script src="../repo/js/bootstrap.js"></script>
<script src="../repo/js/jquery.validate.js"></script>
</head>
<body>
<table>
<thead>
<tr>
<td>Type</td>
<td>Garage</td>
<td>Stock</td>
</tr>
</thead>
<tbody>
<tr>
<td><select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
</td>
<td><select name="garage[]" class="select2 gd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
</td>
<td><input type="text" name="stock[]" class="stock">
</td>
</tr>
<tr>
<td><select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
</td>
<td><select name="garage[]" class="select2 gd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
</td>
<td><input type="text" name="stock[]" class="stock">
</td>
</tr>
<tr>
<td><select name="Type[]" class="select2 pd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
<input type="hidden">
</td>
<td><select name="garage[]" class="select2 gd" style="width: 50%;">
<option value="1">Garage1</option>
<option value="2">Garage2</option>
<option value="3">Garage3</option>
<option value="4">Street parking4</option>
<option value="5">Carport5</option>
</select>
</td>
<td><input type="text" name="stock[]" class="stock">
</td>
</tr>
</tbody>
</table>
<input type="button" name="add" value="Add More" id="addMore" />
<script>
$('table').on('click', '.stock', function(event){
var parentTr = $(this).closest('tr');
var type = $(parentTr).find('td:eq(0) select').val();
var garage = $(parentTr).find('td:eq(1) select').val();
var total = parseInt(type)+parseInt(garage);
$(this).val(total);
});
$('#addMore').click(function(){
var newElement = '<tr><td><select name="Type[]" class="select2 pd" style="width: 50%;"><option value="1">Garage1</option><option value="2">Garage2</option><option value="3">Garage3</option><option value="4">Street parking4</option><option value="5">Carport5</option></select><input type="hidden"></td><td><select name="garage[]" class="select2 gd" style="width: 50%;"><option value="1">Garage1</option><option value="2">Garage2</option><option value="3">Garage3</option><option value="4">Street parking4</option><option value="5">Carport5</option></select></td><td><input type="text" name="stock[]" class="stock"></td></tr>';
$('table tbody').append(newElement)
});
</script>
</body>
</html>