如何将文本从 inputtext 传递到 URL?
How to pass text from inputtext to URL?
我想将输入文本 id = "u_name"
传递给表单操作 url。
<form id = "ismForm" action = {% url 'polls:todo' %} method="get" >
<input type="text" id = "u_name" name="name" />
<input type="text" name="email" />
<input type="text" name="pass" />
{% csrf_token%}
<input type="submit" name="click" value="Register" />
/>
</form>
在提交表单上添加监听器。在提交时将 u_name
值添加到 action
。
const form = document.getElementById("ismForm");
form.addEventListener("submit", function(e){
//check to see if the form is valid before doing this...
this.action += `?u_name=${form.querySelector('#u_name').value}`
})
无需执行 e.preventDefault()
,因为目标是让导航器处理表单提交。
工作示例:(需要 e.preventDefault()
才能看到 console.log
输出)
const form = document.getElementById("ismForm");
form.addEventListener("submit", function(e){
e.preventDefault()
this.action += `?u_name=${form.querySelector('#u_name').value}`
console.log(this.action);
})
<form id = "ismForm" action ="/some/url" method="get" >
<input type="text" id = "u_name" name="name" />
<input type="submit" name="click" value="Register" />
</form>
我想将输入文本 id = "u_name"
传递给表单操作 url。
<form id = "ismForm" action = {% url 'polls:todo' %} method="get" >
<input type="text" id = "u_name" name="name" />
<input type="text" name="email" />
<input type="text" name="pass" />
{% csrf_token%}
<input type="submit" name="click" value="Register" />
/>
</form>
在提交表单上添加监听器。在提交时将 u_name
值添加到 action
。
const form = document.getElementById("ismForm");
form.addEventListener("submit", function(e){
//check to see if the form is valid before doing this...
this.action += `?u_name=${form.querySelector('#u_name').value}`
})
无需执行 e.preventDefault()
,因为目标是让导航器处理表单提交。
工作示例:(需要 e.preventDefault()
才能看到 console.log
输出)
const form = document.getElementById("ismForm");
form.addEventListener("submit", function(e){
e.preventDefault()
this.action += `?u_name=${form.querySelector('#u_name').value}`
console.log(this.action);
})
<form id = "ismForm" action ="/some/url" method="get" >
<input type="text" id = "u_name" name="name" />
<input type="submit" name="click" value="Register" />
</form>