这个代码有什么问题?我在控制台上得到了确切的移动号码,但在/search_user?mob上没有查询路由?
<input type="tel" name="message" id="mobile_input" value="">
<!--getting mobile no. from the input tag -->
<script type="text/javascript" defer>
e => {
e.preventDefault();
var mobInput = document.querySelector('#mobile_input');//get the input tag.
var moby = mobInput.value;//get mobile no.
console.log(moby);//be sure from the variable mob
mobInput.value='';//reset the input tag.
return moby;
}
</script>
<!-- query routing on /search_user?mob -->
<a href="/search_user?mob=moby" class="border-shadow">Search</a>发布于 2022-01-10 21:30:06
我试过这样的方法,这看起来很管用。
<form>
<input type="tel" name="message" id="mobile_input">
<button type="submit" onclick="handleSearch(document.getElementById('mobile_input').value)">Search</button>
</form>
<!--getting mobile no. from the input tag -->
<script type="text/javascript" defer>
// const handleChange = (value) => {
// var mobInput = value
// console.log(mobInput)
//
// }
const handleSearch = (input) => {
var searchValue = input;
console.log("This is the search value " + searchValue)
var url = '/search_user?mob=' + searchValue;
console.log(url)
window.location.href = url;
}
</script>说明:我将href元素更改为一个按钮元素。每次单击按钮(onclick)时,都会调用函数handleSearch。该函数以ID为"mobile_input“的输入元素的值作为输入。当然,您可以更多地清理这个函数。在将url基(‘/search?searchValue=’)与输入值( handleSearch )合并之后,handleSearch函数应该重定向到url (调用window.location.href)。当然,对于服务器的正确调用,您可以更改最后一个调用。希望这能有所帮助。
附带注意:您将看到有一个注释掉的handleChange函数。您可以在输入元素中调用此函数,以跟踪控制台中的更改。这个函数使用onChange调用,就像在搜索按钮中使用onClick一样。欲了解更多信息:https://www.w3schools.com/jsref/event_onchange.asp
https://stackoverflow.com/questions/70646263
复制相似问题