[英]how do I take (req.param ) through input & button instead of typing in URL bar
我正在嘗試從用戶那里獲取輸入並將它們發送到他們輸入的 URL。
通過將它與 url = "http://localhost:3000/"+"/:userInput" 連接起來
首先,我嘗試從用戶那里獲取輸入並使用 DOM 方法創建鏈接
<script>
function myFunction() {
var x = document.getElementById("newLis").value;
document.getElementById("btn").href ='/' + x;
console.dir(x);
}
</script>
並嘗試將其附加到鏈接 href,但沒有成功。
其次,我嘗試通過post路由訪問輸入值,並嘗試直接發送進去
app.get("/:pathName",(req,res)=>{
const pathName = _.capitalize(req.params.pathName);
//console.log(Array.isArray(pathName));
List.findOne({name : pathName}, (error, results)=>{
if (!error){
if(!results){
//prompt("Hello! I am an alert box!");
const list = new List({
name : pathName,
items : def
})
list.save();
List.aggregate();
res.redirect('/' + pathName);
}else {
res.render("list", {listTitle: results.name, newListItems: results.items});
}
}
});
});
但我不知道它是如何工作的。
“newLis”的 id,然后將 id 為“btn”的元素的 href 屬性設置為用戶輸入的值,並在開頭附加“/”。
在發布請求中,獲取用戶使用req.body.name
鍵入的 URL 的值。 連接字符串以獲得完整路徑,然后將用戶重定向到該路徑
app.post("/yourpath",(req,res)=>
{
const path= "/"+req.body.name;
res.redirect("path");
}
這里的名稱是輸入標簽中名稱屬性的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.