英文:
How can I bring input field texts to address bar?
问题
If I type some texts in the input field, how can I bring those texts to address bar?
for eg: I type abcd to input field, I need address bar like www.google.com/abcd
英文:
If I type some texts in the input field, how can I bring those texts to address bar ?
for eg : I type abcd to input field, I need address bar like www.google.com/abcd
答案1
得分: 0
这部分是您要翻译的内容:
This can work:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<body>
<p>Typing something...</p>
<input type="text" id="Input1" onkeyup="displayResult()"><!--onkeypress can not function backspace-->
<br>
<button id="Hite">
Hite
</button>
<script>
function displayResult()
{
var text = "www.google.com";
var Input1Value = document.getElementById("Input1").value;
document.getElementById("Hite").innerHTML = text + "\\"+ Input1Value + "\\";
}
</script>
<!-- end snippet -->
I using onkeyup event so when the value of the input changes, it will function to set the text. Also, the reason why I do not using onkeypress is: onkeypress can not function when you press backspace.
Then, if you want to get the address, you can use document.getElementById("Hite").innerHTML to get it (As you did not required to get it)
英文:
This can work:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<body>
<p>Typing something...</p>
<input type="text" id="Input1" onkeyup="displayResult()"><!--onkeypress can not function backspace-->
<br>
<button id="Hite">
Hite
</button>
<script>
function displayResult()
{
var text = "www.google.com";
var Input1Value = document.getElementById("Input1").value;
document.getElementById("Hite").innerHTML = text +"\\"+ Input1Value + "\\";
}
</script>
<!-- end snippet -->
I using onkeyup event so when the value of the input changes, it will function to set the text. Also, the reason why I do not using onkeypress is: onkeypress can not function when you press backspace.
<br><br>
Then, if you want to get the address, you can use document.getElementById("Hite").innerHTML to get it (As you did not required to get it)
答案2
得分: 0
尝试这个:
function updateAddressBar() {
const inputValue = document.getElementById("inputField").value;
window.history.replaceState({}, '', `?value=${inputValue}`);
}
oninput
事件用于在输入字段的值更改时调用updateAddressBar
函数。该函数使用document.getElementById
来获取输入字段的值,然后使用window.history.replaceState
来使用输入字段的新值更新URL。
英文:
Try this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function updateAddressBar() {
const inputValue = document.getElementById("inputField").value;
window.history.replaceState({}, '', `?value=${inputValue}`);
}
<!-- language: lang-html -->
<form>
<input type="text" id="inputField" oninput="updateAddressBar()">
</form>
<!-- end snippet -->
The oninput event is used to call the updateAddressBar function whenever the value of the input field changes. The function uses document.getElementById to get the value from the input field, and then window.history.replaceState to update the URL with the new value from the input field.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论