【发布时间】:2017-01-20 14:19:14
【问题描述】:
我正在尝试有一个功能,用户可以从下拉框中选择他们想要的项目,然后价格将显示在其侧面的输入框中。
我现在面临在多行中具有相同功能的问题。代码如下:
<td class="formiterate" >
<select id="Employee_ID" name="id[]">
<option value="">Select one</option>
<?php
$st = $pdo->prepare("SELECT * FROM tbl_stock");
$st->execute();
$rowes = $st->fetchAll(PDO::FETCH_ASSOC);
foreach ($rowes as $rowe) {
?><option value="<?= $rowe ['id']; ?>"><?= $rowe ['stock_item']; ?> (<?= $rowe ['stock_brand']; ?>)</option><?php
}
?>
</select>
</td>
<td class="formiterate"><input type="text" name="Last_name[]" id="Last_name"></td>
如您所见,当您从Employee_ID 中选择时,商品价格将反映在Last_name[] 输入框中。
虽然代码适用于单行,但我无法为多行迭代函数。
下面是我的javascript:
$(function() { // This code will be executed when DOM is ready
$('#Employee_ID').change(function() { // When the value for the Employee_ID element change, this will be triggered
var $row = $(this); // We create an jQuery object with the select inside
$.post("getData.php", { Employee_ID : $row.val()}, function(json) {
if (json && json.status) {
$('#Last_name').val(json.lastname);
}
})
});
})
我尝试在var ($row)= $this 处添加.closest(".rows"),但注意到发生了。
请帮忙。
谢谢
【问题讨论】:
-
而不是
<select id="Employee_ID"使用<select class="Employee_ID"然后$('.Employee_ID')。这样做 -
id 用于单元素事件处理,class 用于组元素事件处理
-
感谢@Anant 先生的快速回复!但是现在,每次我使用下拉菜单进行选择时,该值都会影响其他输入框。我只希望下拉菜单只影响与其一致的输入框
标签: javascript php json select dropdown