Javascript 如何在<;中获取搜索框值;p>;标签-HTML,JS

Javascript 如何在<;中获取搜索框值;p>;标签-HTML,JS,javascript,html,button,input,search-box,Javascript,Html,Button,Input,Search Box,我是javascript新手,在搜索框中输入pincode将显示在标签中!!如何做?谢谢你的帮助。提前谢谢 HTML: <form class="form1" role="search" method="get" action="search-pincode.html"> <input class="pincode1" type="number" placeholder="Enter Postcode" id="user-pincode"> <button clas

我是javascript新手,在搜索框中输入pincode将显示在标签中!!如何做?谢谢你的帮助。提前谢谢

HTML:

<form class="form1" role="search" method="get" action="search-pincode.html">
<input class="pincode1" type="number" placeholder="Enter Postcode" id="user-pincode">
<button class="submit" type="submit" formaction="search-pincode.html" onclick="myFunction()">Check</button>
</form>
    <script>
    function myFunction() {
    var x = document.getElementById("user-pincode").value;
    document.getElementById("user-pincode-show").innerHTML = x;
    }
    </script>

检查
JS脚本:

<form class="form1" role="search" method="get" action="search-pincode.html">
<input class="pincode1" type="number" placeholder="Enter Postcode" id="user-pincode">
<button class="submit" type="submit" formaction="search-pincode.html" onclick="myFunction()">Check</button>
</form>
    <script>
    function myFunction() {
    var x = document.getElementById("user-pincode").value;
    document.getElementById("user-pincode-show").innerHTML = x;
    }
    </script>

函数myFunction(){
var x=document.getElementById(“用户pincode”).value;
document.getElementById(“用户pincode显示”).innerHTML=x;
}
输出:搜索pincode.html

                <h1 class="search-h1-contact">
                Yes, we install CCTV in <p id="user-pincode-show"></p>
                </h1>

是的,我们在


首先,您将看不到足够长的结果,因为单击按钮后,您将被重定向到
search pincode.html
。尝试将
type=“submit”
更改为
type=“button”

函数myFunction(){
var x=document.getElementById(“用户pincode”).value;
document.getElementById(“用户pincode显示”).innerHTML=x;
}

检查
是的,我们在北京安装了闭路电视


如果要避免在表单提交时重定向,可以使用声明事件侦听器来处理单击事件,并在函数处理程序中调用
e.preventDefault

还请注意,不需要在
按钮
元素中添加
formaction=“search pincode.html”
,因为
form
具有属性
action=“search pincode.html”

最后一件事:在
h1
元素中,您可以更好地使用
span
元素,而不是
p

<h1>Yes, we install CCTV in <span id="user-pincode-show"></span></h1>

检查
是的,我们在
函数myFunction(){
var x=document.getElementById(“用户pincode”);
document.getElementById('user-pincode-show')。innerHTML=x.value;
}

检查
是的,我们在北京安装了闭路电视

太棒了!它起作用了。你能建议我哪种方法传递值更好吗?好吧,如果它只是在同一个页面中传递值,这就足够了,或者使用Jquery来获得一个较短的版本,但仍然使用相同的功能是的,在每个页面中使用搜索框现在再次尝试从索引页面传递值。它不起作用:'(很好,转到Jquery