当用户单击并选择客户端时,我想根据客户端更改区域,然后区域下拉列表根据数据库中与该客户端相关的区域进行更改在此处输入代码
这是我的客户表:
这是我的区域表,其中 client_id 是外键:
这是我的代码
<div class="container">
<h3>Add Area</h3>
<form action="/add_areas" method="POST">
<!--input type="text" placeholder="DistributorID" name="distributor_id"-->
<input type="text" placeholder="Area Name" name="area_name" required="">
<!-- <input type="text" placeholder="Contact ID" name="contact_id" required=""> -->
<!-- <label>Client ID</label> -->
<select name="client_id" required="">
<option disabled selected value> -- select a Client -- </option>
<%for(i=0; i<clients.length; i++){%>
<option value="<%= clients[i].id%>"><%= clients[i].client_name %></option>
<%}%>
</select>
<select name="region_id" required="">
<option disabled selected value> -- select a Region -- </option>
<%for(i=0; i<regions.length; i++){%>
<option value="<%= regions[i].id%>"><%= regions[i].region_name %></option>
<%}%>
</select>
<br>
<button type="submit">Submit</button>
</form>
<br>
<%if(areas.length>0) { %>
<h3>All Existing Areas</h3>
<!--For view-->
<div class="table-rsponsive">
<table class="table table-striped table-bordered table-sm " cellspacing="0" width="100%">
<tr>
<th>Area Name</th>
<th>Client </th>
<th>Region </th>
<th colspan="2">Action</th>
</tr>
<% for(var i=0; i<areas.length; i++) {%>
<tr>
<td><%= areas[i].area_name %></td>
<td><%= areas[i].client_name %></td>
<td><%= areas[i].region_name %></td>
<td><a href="/edit_areas/<%= areas[i].id %>"><button class="btn-primary">Edit</button></a></td>
<td><a href="/delete_areas/<%= areas[i].id %>" onclick="javascript: return confirm('Are you SURE? If you delete this area All the town belongs to <%= areas[i].area_name %> area will automatically deleted!!');"><button class="btn-danger">Delete</button></a></td>
</tr>
<% } %>
</table>
</div>
<% }
else{ %>
<h3>No Area Found!!</h3>
<% } %>
<!--goto dashboard button-->
<button type="submit" class="btn btn-primary btn-block" onclick="window.location.href='/dashboard.html'">Return to dashboard</button>
</div>
这是添加区域的页面: Here is the page to add area
最佳答案
您需要在 onChange 事件上为 field_id=client_id 添加 ajax 请求,以根据选定的 client_id 获取区域。对于响应,您可以根据需要设计服务器,您可以在服务器端生成 HTML,也可以从数据库中获取原始结果作为响应,然后在客户端生成 HTML。当您获得适合选择选项的 HTML 格式后,将该 html 附加到 region_id 选择。
关于javascript - Nodejs mysql 中的动态下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929388/