2 回答

TA貢獻1858條經驗 獲得超8個贊
有很多方法可以實現它。
利用模板字符串和提示的單行方法(創建一個彈出窗口,提示用戶輸入某些內容)。
window.location.replace(`http://${prompt('Input:')}.example.com`)
使用形式及其onsubmit
屬性(您的方法)。請注意,對于 ES6,請使用let或const而不是var
。
function resetId(){
? ? const idInput = document.getElementById("id");
? ? const url = `http://${idInput.value}.example.com`;
? ? alert("Value before submit: " + url);
? ? window.location.replace(url);
}
<form action="#" method="post" onsubmit="resetId();" >
? ? <input type="text" name="id" value="domain" id="id">
? ? <input type="submit">
</form>
一種不使用表單而是使用按鈕的方法。
// Same as above
function resetId(){
? ? const idInput = document.getElementById("id");
? ? const url = `http://${idInput.value}.example.com`;
? ? alert("Value before submit: " + url);
? ? window.location.replace(url);
}
<div>
? ? <label for="id">Input:</label>
? ? <input type="text" name="id" value="domain" id="id">
</div>
<button onclick="resetId()">submit</button>
注意:不要<br />在表單中使用新行,而是考慮對每行使用<label>并<input>包裹在<div>如下所示的內容中:
<div>
? ? <label for="something">Something:</label>
? ? <input type="text" name="something" id="something" />
</div>
添加回答
舉報