[英]PHP MYSQL Multiple drop downs Select value on change
I have fetched product sizes and quantity from a table and showed them in dropdown lists.Depending on products the drop downs may vary from 5 to 6.I want to get the value of size id from the drop down when a user selects it from the drop down and put it as value in the input type number. 我已经从表格中获取了产品尺寸和数量,并在下拉列表中显示。根据产品的不同,下拉菜单的范围可能从5到6。我想从用户下拉菜单中获取尺寸ID的值下拉列表并将其作为值输入到输入类型编号中。
<select name='size' id='size' class='form-control'>
<option value="1">Small</option>
</select>
<input type='number' name='quantity' min='1' value='1' class='form-control' />
<select name='size' id='size' class='form-control'>
<option value="2">Large</option>
</select>
<input type='number' name='quantity' min='1' value='1' class='form-control' />
<select name='size' id='size' class='form-control'>
<option value="3">XLarge</option>
</select>
<input type='number' name='quantity' min='1' value='1' class='form-control' />
The jquery I am using is as follows, 我正在使用的jQuery如下,
<script type="text/javascript">
$(document).ready(function() {
$("#size").change(function() {
var str = "";
$("select option:selected").each(function() {
str += $(this).val() + " ";
});
});
});
</script>
I am getting values form first dropdown but can not get from other dropdowns. 我从第一个下拉列表中获取值,但无法从其他下拉列表中获取值。 Kindly help
请帮助
As discussion i did a fiddle to give the solution Fiddle Link 由于讨论中,我做了一个小提琴给予解决小提琴链接
Updated JS: 更新的JS:
$(document).ready(function() {
$(".size").on("change", function() {
$(this).closest("div").find("input").val($(this).val());
});
});
If you want to get the $row['size']
when select any option then you have to change your PHP
code also. 如果要在选择任何选项时获取
$row['size']
,则还必须更改PHP
代码。
Updated PHP 更新的PHP
<div>
<select name='size' class='form-control size'>
<?php foreach ($rows as $row) {?>
<option value="<?php echo $row['size']?>" <?php if ($row['quantity'] == 0) echo "disabled=disabled"?>>
<?php echo $row['size']?>
</option>
<?php }?>
</select>
<input type='number' name='quantity' min='1' value='1' class='form-control' />
</div>
<div>
<select name='size' class='form-control size'>
<?php foreach ($rows as $row) {?>
<option value="<?php echo $row['size']?>" <?php if ($row['quantity'] == 0) echo "disabled=disabled"?>>
<?php echo $row['size']?>
</option>
<?php }?>
</select>
<input type='number' name='quantity' min='1' value='1' class='form-control' />
</div>
Note:
$(this).val()
get the selected value from the drop down and put the value into the input field by$(input[name=quantity]).val()
I put the$row['size']
to theoption value
注意:
$(this).val()
从下拉列表中选择值,然后通过$(input[name=quantity]).val()
将值放入输入字段中$(input[name=quantity]).val()
我将$row['size']
放入到option value
Let me know is it helpful to you or not. 让我知道这对您有没有帮助。
try this: 尝试这个:
<td>
<select name='size' id='size' class='form-control'>
<?php foreach ($rows as $row) {?>
<option value="<?php echo $row['id']?>" <?php if ($row['quantity'] == 0) echo "disabled=disabled"?> >
<?php echo $row['size']?>
</option>
<?php }?>
</select>
</td>
<td>
<input type='number' name='quantity' min='1' value='1' class='form-control' />
</td>
您的下拉ID应该不同,因为您知道ID对于文档中的一个元素是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.