douxiapi4381 2017-01-10 14:39
浏览 55

在页面加载时自动显示表单值或使用.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. 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?

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.

  • 写回答

1条回答 默认 最新

  • douqixia7942 2017-01-10 14:43
    关注

    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,

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大