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;
}
添加回答
舉報