如何防止显示搜索输入名称

时间:2017-11-09 06:18:26

标签: javascript html

我比较新,学习绳索。下面的代码工作正常,但我不知道如何防止搜索输入名称显示在页面上。图片也附在下面。感谢您的帮助。

<h1>Enter Name</h1>
<form method="get">
    <p>
        <label for="requestName">Enter Your Name:</label>
        <input type="search" name="nameEntered" id="nameEntered">
        <input type="submit" name="submit" id="submit" value="Submit">
    </p>
</form>

<script type="text/javascript">
    n=document.location.search.substring(0);
    document.write("<h3>")

    n1=document.location.search.substring(1);

    n2=new Array();
    n2=n1.split('&');

    document.write("You entered the name " + n2[0]);
    document.write("</h3>")
</script>

截图:

enter image description here

3 个答案:

答案 0 :(得分:0)

任何document.write()函数基本上都会写入客户端的HTML文档。从代码中可以看出,它正在编写<h3>开始和结束标记,还有"You have entered the name " + n2[0]

删除它们,您将不会将任何数据写入页面。

答案 1 :(得分:0)

您可以进一步拆分结果字符串以删除名称:

<script type="text/javascript">
    n=document.location.search.substring(0);
    document.write("<h3>")

    n1=document.location.search.substring(1);

    n2=new Array();
    n2=n1.split('&');

    n3=new Array();
    n3=n2[0].split('=');

    document.write("You entered the name " + n3[1]);
    document.write("</h3>")
</script>

你可以添加&#34; =&#34;或&#34;:&#34;如果喜欢:

document.write("You entered the name: " + n3[1]);

答案 2 :(得分:0)

i = 0
arr[i] == arr[0] == 1
arr[i+2] == arr[2] == 6
arr[i] = arr[0] + arr[2] = 1 + 6 = 7

尝试这应该可以胜任。