【发布时间】:2021-06-27 21:04:05
【问题描述】:
控制器:
$school_edit = SchoolData::find($school_id);
阿贾克斯:
$('#edit_school_name').val(response.school_edit.school_name);
$('#edit_school_description').val(response.school_edit.school_description);
$('#edit_cat_id').val(response.school_edit.school_id);
$('#edit_subject_name').val(response.school_edit.subject_name);
表格:
<input type="text" id="edit_school_name" class="form-control form-group w-100" placeholder="School Name">
<textarea id="edit_school_description" class="form-control w-100" placeholder="School Description" cols="50" rows="10"></textarea>
我当前的代码输出 school_name 和 school_description 没有任何问题,但我如何在下拉列表中执行相同操作?例如,选项是“数学”和“科学”,在表格数据中,选择了“科学”,我需要在我的编辑中将第一个选项显示为“科学”。
所以基本上,如果选择“科学”,它将看起来像:
<select id="edit_subject_name">
<option>(selected subject from database)</option>
<option>Math</option>
<option>Science</option>
</select>
通常,在添加表单中,只有数学和科学两个选项。编辑表单将有三个,编辑中的第一个选项将始终是从数据库中选择的。
我知道我不能简单地这样做:
<option id="edit_subject_name"></option>
<option>Math</option>
<option>Science</option>
因为选项内容会是空的。
任何帮助将不胜感激。
【问题讨论】:
-
主题是否有 ID 或 slug?如果没有,您可以将名称转换为 slug,然后将其作为选项的值传递,然后您可以比较以检查下拉列表中选择的内容
标签: ajax laravel html-select crud edit