I got a a dropdown list populating from a php while loop. There will be more than one select dropdown boxes. When i select any dropdown from the loop, the subsequent textbox should change the value.
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$("#category").change(function () {
var dept_number = $(this).val();
var price = $(this).find(':selected').data('price');
$('#dept-input').val(dept_number);
$('#price-input').val(price);
});
});//]]>
</script>
</head>
<body>
<select id="category[]">
<option value="1" data-price="10">Item 1 second</option>
<option value="2" data-price="20">Item 2 second</option>
<option value="3" data-price="30">Item 3 second</option>
</select>
<br><br>
<label>Dept. num:</label>
<input type="text" id="dept-input[]"></input>
<br><br>
<label>Price:</label>
<input type="text" id="price-input[]"></input>
</body>
</html>
This script is working with out the loop. How will i change it to work with the loop.. Thank you in advance
<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function(){
$("select").on('change',function () {
var dept_number = $(this).val();
var price = $(this).find(':selected').data('price');
$(this).siblings('.deptip').val(dept_number);
$(this).siblings('.priceip').val(price);
$(this).siblings('.total1').val(price * $(this).siblings('.total').data('value'));
});
});
});//]]>
</script>
<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
<td><select id="category0">
<option value="1" data-price="10">Item 1 second</option>
<option value="2" data-price="20">Item 2 second</option>
<option value="3" data-price="30">Item 3 second</option>
</select></td>
<td><label>Dept. num:</label>
<input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
<td><label>Price:</label>
<input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
<td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
</tr></table>
</div>
<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
<td><select id="category1">
<option value="1" data-price="10">Item 1 second</option>
<option value="2" data-price="20">Item 2 second</option>
<option value="3" data-price="30">Item 3 second</option>
</select></td>
<td><label>Dept. num:</label>
<input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
<td><label>Price:</label>
<input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
<td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
</tr></table>
</div>
<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
<td><select id="category2">
<option value="1" data-price="10">Item 1 second</option>
<option value="2" data-price="20">Item 2 second</option>
<option value="3" data-price="30">Item 3 second</option>
</select></td>
<td><label>Dept. num:</label>
<input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
<td><label>Price:</label>
<input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
<td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>
</tr></table>
</div>