gpt4 book ai didi

javascript - 动态下拉菜单 Node js

转载 作者:行者123 更新时间:2023-11-29 16:13:32 25 4
gpt4 key购买 nike

我是 Node 新手。我需要制作一个依赖的下拉菜单,它从同一页面中的另一个下拉菜单中获取所选用户的地址数据。问题是孔页面不仅更新了第二个下拉菜单。我认为这是同样的问题如dynamically dropdown in nodejs mysql ,但这对我没有多大帮助。

<select name="selectUser" id="user" >
<option disabled selected> Select User..</option>
<% users.forEach((users) => { %>
<option value="<%= users.id %>" > <%=users.name %> </option>
<% }) %>
</select>
<br>
<label>Address :</label>
<select name="selectAddress" id="address">
<option disabled selected> Select Address..</option>
<% address.forEach((address) => { %>
<option value="<%= address.addressId %>" > <%=address.addressName %> </option>
</select>
<% }) %>

我的ajax请求:

 $(document).ready(function(){
$('#user').change(function(){

var item = $('#user').val();
var add = $('#address').val();
$.ajax({
type:'GET',
data: {selectedId : item },
url:'/order/new',
success: function(result1){
$('#body').html(result1);

}
});
});
});

order.js

 module.exports = {
addOrderPage: (req, res) => {
let query1 = "SELECT * FROM users";
getConnection().query(query1, (err, result1) => {
let query2 = "SELECT * FROM address WHERE userId = '" +req.query.selectedId + "'";
getConnection().query(query2, (err, rows, fields) => {
if (err) {
return res.status(500).send(err);
}
console,log(rows)
res.render('newOrder.ejs', {
address : rows,
users: result1
});
});
});
}
}

app.js

app.get('/order/new', addOrderPage)

最佳答案

您的成功函数缺少从请求更新您的成功函数返回的数据

       $(document).ready(function(){
$('#user').change(function(){

var item = $('#user').val();

$.ajax({
type:'GET',
data: { selectedId: item },
url:'/users/address',
success: function(data){
console.log(data);
$('#address').empty();
$('address').append("<option disabled selected> Select Address..</option>");
$.each(data, function (index, addressObj) {
$('#address').append("<option value = '" + addressObj.id + "' > " + addressObj.first_name + ". </option > ");
});
}
});
});

在 order.js 中,您需要为用户创建一个调用,并且一个调用用于用户地址数据:-

      module.exports = {
addOrderPage: (req, res) => {
var selecteduser = req.query.selectedId;
let query1 = "SELECT * FROM users";
db.query(query1, (err, result1) => {
if (err) {
return res.status(500).send(err);
}
res.render('newOrder.ejs', {
players: result1,
});
});
},
getUserAddress: (req, res) => {
var selecteduser = req.query.selectedId;
let query1 = "SELECT * FROM address WHERE userId = '" + selectedId + "'";
db.query(query1, (err, result1) => {
if (err) {
return res.status(500).send(err);
}
res.send(result1);
});
}
}

neworder.js

 <select name="selectUser" id="user" >
<option disabled selected> Select User..</option>
<% users.forEach((users) => { %>
<option value="<%= users.id %>" > <%=users.name %> </option>
<% }) %>
</select>
<br>
<label>Address :</label>
<select name="selectAddress" id="address">
<option disabled selected> Select Address..</option>
</select>

在你的app.js或index.js中你需要添加它的路由

 app.use("/users/address", order.getUserAddress); 

关于javascript - 动态下拉菜单 Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55044085/

25 4 0