【发布时间】:2021-09-10 20:59:32
【问题描述】:
我创建了一个用于输入产品数据的表单,它将结果发送回 mysql 数据库。我有一个名为“attribute_name”的下拉/选择 ID 和另一个“attribute_value”,我设法将结果发送回数据库,效果很好。但是,我想限制用户仅根据名称选择值,因此如果他们选择 size 他们将只有小、中、大而不是黑色、迷彩、紫色的值来自 颜色,反之亦然。
底部的脚本适用于第一行,但不适用于屏幕截图中显示的任何其他行。
工作
不工作
代码
<?php
include 'assets/processes/db-connection.php';
$query = "SELECT *
FROM attribute_name;";
$query_run = mysqli_query($conn, $query);
$att_name = "<select name='attribute_name' id='attribute_name' class='form-control country'>";
$att_name .= "<option value='0'></option>";
while ($row = mysqli_fetch_assoc($query_run)) {
$att_name .= "<option value='{$row['attribute_name']}'>{$row['attribute_name']}</option>";
}
$att_name .= "</select>"
?>
<?php
include 'assets/processes/db-connection.php';
$query = "SELECT `attribute`.*, `attribute_name`.*
FROM `attribute`
LEFT JOIN `attribute_name` ON `attribute`.`attribute_name_id` = `attribute_name`.`attribute_name_id`;";
$query_run = mysqli_query($conn, $query);
$att_value = "<select name='attribute_id' id='attribute_value' class='form-control'>";
$att_value .= "<option value='0'></option>";
while ($row = mysqli_fetch_assoc($query_run)) {
$att_value .= "<option data-parent='{$row['attribute_name']}' value='{$row['attribute_id']}'>{$row['attribute_value']}</option>";
}
$att_value .= "</select>"
?>
<div id="wrapper">
<div id="form_div">
<table class="table table-hover text-nowrap" id="attribute_table">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th colspan="1">Action</th>
</tr>
</thead>
<tr id="row1">
<td>
<?php echo $att_name; ?>
</td>
<td>
<?php echo $att_value; ?>
</td>
<td><input class="btn btn-block btn-primary" type="button" onclick="add_attribute_row();" value="+"></td>
</tr>
</table>
</div>
</div>
<script>
$('#attribute_name').change(function() { var parent = $(this).val(); $('#attribute_value').children().each(function() { if ($(this).data('parent') != parent) { $(this).hide(); } else $(this).show(); }); });
</script>
此代码经过编辑以在 stackoverflow 上使用
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="wrapper">
<div id="form_div">
<table class="table table-hover text-nowrap" id="attribute_table">
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th colspan="1">Action</th>
</tr>
</thead>
<tr id="row1">
<td>
<select name='attribute_name' id='attribute_name' class='form-control'>
<option value='0'></option>
<option value='Colour'>Colour</option>
<option value='Size'>Size</option>
</select>
</td>
<td>
<select name='attribute_id' id='attribute_value' class='form-control'>
<option value='0'></option>
<option data-parent='Colour' value='1'>Black</option>
<option data-parent='Colour' value='2'>Camo</option>
<option data-parent='Colour' value='3'>Purple</option>
<option data-parent='Size' value='4'>Small</option>
<option data-parent='Size' value='5'>Medium</option>
<option data-parent='Size' value='6'>Large</option>
</select>
</td>
<td><input class="btn btn-block btn-primary" type="button" onclick="add_attribute_row();" value="+"></td>
</tr>
</table>
</div>
</div>
<script>
function add_attribute_row() {
$rowno = $("#attribute_table tr").length;
$rowno = $rowno + 1;
$("#attribute_table tr:last").after("<tr id='row" + $rowno + "'><td><select name='attribute_name' id='attribute_name' class='form-control'><option value='0'></option><option value='Colour'>Colour</option><option value='Size'>Size</option></select></td><td><select name='attribute_id' id='attribute_value' class='form-control'><option value='0'></option><option data-parent='Colour' value='1'>Black</option><option data-parent='Colour' value='2'>Camo</option><option data-parent='Colour' value='3'>Purple</option><option data-parent='Size' value='4'>Small</option><option data-parent='Size' value='5'>Medium</option><option data-parent='Size' value='6'>Large</option></select></td><td><input class='btn btn-block btn-primary' type='button' value='-' onclick=del_att_row('row" + $rowno + "')></td></tr>");
}
function del_att_row(rowno) {
$('#' + rowno).remove();
};
</script>
<script>
$('#attribute_name').bind('change', function () {
var parent = $(this).val();
$('#attribute_value').children().each(function () {
if ($(this).data('parent') != parent) {
$(this).hide();
} else
$(this).show();
});
});
</script>
【问题讨论】:
-
您实际上只是在自己在线创建... $att_name 应该变为:$att_name[0] 例如:$att_name[0] = "
-
欢迎来到 Stack Overflow。请提供一个最小的、可重现的示例:stackoverflow.com/help/minimal-reproducible-example 您当前的示例无法复制,甚至不提供示例数据。
-
@Shlomtzion 我添加了 $i=0;和 ++$i 在每个 while 循环的末尾,但是当我添加新行时它对下拉选择没有影响。
标签: javascript php html jquery