简体   繁体   English

在页面加载时自动显示表单值,或使用.innerHTML提交

[英]Automatically show form value on page load or submit using .innerHTML

I'm trying to find the simplest, non-expert-coder solution to displaying a search query. 我试图找到最简单的非专家编码器解决方案来显示搜索查询。

I have a search form where onclick of the submit button it displays the form value underneath. 我有一个搜索表单,其中“提交”按钮的onclick会在下面显示表单值。 This is the code: 这是代码:

<input type="text" name="searchfield" id="searchfield" value="" />
<input type="submit" name="submit" id="submitSearch" value="Submit" onclick="document.getElementById('output').innerHTML = document.getElementById('searchfield').value" />
<div id="output"></div>

The page reloads on submit rather than going to another page. 该页面在提交时重新加载,而不是转到另一个页面。

Is there a simple way to manipulate my code do display the value in the output div automatically upon page load , rather than onclick? 有没有一种简单的方法来处理我的代码,以便在加载页面时自动输出div中显示值,而不是onclick呢?

So whatever was placed in the search box will automatically be displayed once the page loads after refresh or submit. 因此,刷新或提交后页面加载后,放置在搜索框中的所有内容都会自动显示。 If nothing was entered, then nothing will show. 如果未输入任何内容,则不会显示任何内容。

There are many ways to achieve this, but this would do the job: 有很多方法可以实现这一目标,但是可以做到这一点:

<input type="text" name="searchfield" id="searchfield" value="" />
<button onclick="document.getElementById('output').innerHTML = document.getElementById('searchfield').value">Submit</button>
<div id="output"></div>

And using PHP, 并使用PHP,

<input type="text" name="searchfield" id="searchfield" value="" />
<input type="submit" name="submit" id="submitSearch" value="Submit" />
<div id="output">
<?php
if($_GET['searchfield'])
    echo $_GET['searchfield'];
?>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM