3 回答
TA贡献2065条经验 获得超14个赞
答案是您必须在获取价值代码之前使用您的网址。
function func(){ window.location = "https://example.com/"+ document.getElementById('link-box').value; }
或者
function func(){ var searchKey = document.getElementById('link-box').value; window.location = "https://example.com/"+ searchKey; }
如果您还想从其他输入中添加...您可以这样做
function func(){
var searchKey = document.getElementById('link-box').value;
var searchKey2 = document.getElementById('link-box2').value;
if(!searchKey2){
window.location = "https://example.com/"+ searchKey;
}else{
window.location = "https://example.com/"+ searchKey + "/" + searchKey2;
}
}
TA贡献1784条经验 获得超8个赞
请试试这个。
HTML:
<input type="text" id="link-box"/>
<input type="button" id="search-button" value="Search" onclick="search()"/>
JS:
<script>
const search=()=>{
let search-value = document.getElementById('link-box').value;
if(!search-value){
window.location = `https://example.com/${firstInput}`;
}
return;
}
</script>
TA贡献1900条经验 获得超5个赞
// On input button click, call function func();
<input type="button" id="search-button" value="Search"
onclick="func()"/>
<script>
function func() {
// Gets the values of the 2 input fields.
let firstInput = document.getElementById('link-box1').value;
let secondInput = document.getElementById('link-box2').value;
// Redirect the user to the specified URL with the values from the input boxes.
window.location = 'https://example.com/' + firstInput + '/' + secondInput;
}
添加回答
举报