gpt4 book ai didi

node.js - Nodejs使用ajax根据第一个下拉列表选择的值填充下拉列表

转载 作者:太空宇宙 更新时间:2023-11-04 01:19:47 25 4
gpt4 key购买 nike

我最近提出了一个关于这个类似主题的问题,但它是在 ReactJS 中实现的,现在我需要在 NodeJS 中实现。

这是 ReactJS 中的链接, https://stackoverflow.com/a/59693724/12693684

我试图根据第一个下拉列表选择的值填充第二个下拉列表,第二个下拉列表应使用 JSON API 填充。我目前正在使用 Express 和 Handlebars。

有没有办法在 NodeJS 中做到这一点,或者我应该调用静态页面并使用 jQuery 来完成任务?

这就是我到目前为止所做的

国家.hbs

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Country-State</title>
<!--Load bootstrap.css file-->
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Country-State</h2>
<div class="form-row">
<div class="col">
<label>Choose your country</label>
<br/><br/>
<select id="sel">
<option value="">Select</option>
<option value="Malaysia">Malaysia</option>
<option value="Korea">Korea</option>
</select>
<select id="type" name="select">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
</div>
<!--Load bootstrap.js file-->
<script src="js/bootstrap.js"></script>
</body>

index.js

app.get('/countrystate', (req,res) => {
const url="http://localhost:4000/db";
const fetchData = async url => {
try
{
const response = await fetch(url);
const result = await response.json();
console.log(result)
}
catch (error)
{
console.log(error)
}
}

fetchData(url);
res.render('country');
});

提前谢谢您!

最佳答案

我不知道我是否完全理解你的问题,但你可以通过以下方法在静态页面country.hbs中传递这些数据

填充使用: res.render('country', fetchData)然后您可以在 hbs 文件中接收它并将其映射到该下拉列表中

关于node.js - Nodejs使用ajax根据第一个下拉列表选择的值填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59745890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com