dongyuxiao6295 2015-03-10 21:01
浏览 160

使用php或javascript清除html表单数据

I've got kind of a situation here : I have the following form :

<form action="sample.php" id="searchform" method="post">
<input type="text" id="key_words" name="key_words" value="<?php echo isset($_POST['key_words']) ? $_POST['key_words'] : '' ?>"style="width:377px;">
<input type="text" name="minimum_price" value="<?php echo isset($_POST['minimum_price']) ? $_POST['minimum_price'] : '' ?>">
<input type="text" name="maximum_price" value="<?php echo isset($_POST['maximum_price']) ? $_POST['maximum_price'] : '' ?>">

I'm using the php script in value because i need to keep the value in text box persistent. So, now i need to clear the values in text box when i click a button:

<button type="reset" value="clear" onclick="clearform()">Clear</button>

I've tried a few things and failed. Help please? JavaScript can also be used for clearform() method.

  • 写回答

2条回答 默认 最新

  • doudui6756 2015-03-10 21:19
    关注

    You just need to get the elements by id and set the value attribute to empty string:

    function clearform()
    {
      document.getElementById('key_words').value = '';
      //same thing for other ids
    }
    

    For your minimum_price and maximum_price you need to add an id since you only have name right now.

    Also in this case since you don't want to use HTML's standard reset functionality don't make the button type reset.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog