使用 jquery 获取多个 select2 输入的值
Multiple select2 input's value getting using jquery
我有一些输入字段 class 与 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);
});
请查找以下内容
<!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>
尝试了解
的基础知识
var parentTr = $(this).closest('tr');
var type = $(parentTr).find('td:eq(0) select').val();
var garage = $(parentTr).find('td:eq(1) select').val();
closest() 在 DOM 树上向上移动,而不是向下移动,因此如果 $(this) 等于当前行,您将永远不会使用 closest('td') 在其中找到 td 标签.
假设您不能像 Jamie 所建议的那样简单地使用 id 属性直接访问输入,您将需要使用:
$(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();
(第一个更快)
我有一些输入字段 class 与 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);
});
请查找以下内容
<!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>
尝试了解
的基础知识 var parentTr = $(this).closest('tr');
var type = $(parentTr).find('td:eq(0) select').val();
var garage = $(parentTr).find('td:eq(1) select').val();
closest() 在 DOM 树上向上移动,而不是向下移动,因此如果 $(this) 等于当前行,您将永远不会使用 closest('td') 在其中找到 td 标签.
假设您不能像 Jamie 所建议的那样简单地使用 id 属性直接访问输入,您将需要使用:
$(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();
(第一个更快)