提交表单后如何清除上一个文本字段的值,而不刷新整个页面? [英] How do I clear the previous text field value after submitting the form with out refreshing the entire page?

查看:52
本文介绍了提交表单后如何清除上一个文本字段的值,而不刷新整个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 javascript html 做一个Web应用程序,该应用程序具有包含文本字段按钮的表单.当我在该文本字段中输入数字并单击该按钮提交时,将动态生成文本区域.提交表单后,将创建一些文本区域,但是如果我对现有文本区域不满意,那么我将再次输入一些值而无需刷新页面.但是以前输入的文本字段值优先于页面上现有文本区域下方的新文本区域.

I am doing a web application using javascript and html that has a form containing a text field, button. When I enter a number in that text field and submit by clicking on that button, text areas are generated dynamically. Once my form is submitted some text areas are created but if I am not satisfied with existing text areas then again I enter some value with out refreshing page. But the text field value entered previously prevails showing the new text areas below the existing text areas on the page.

因此,我如何在不刷新页面的情况下清除值.

<div>
    <html>
     <input type="text" name = "numquest" id ="numquest" value="" size="5" style="" disabled>
     <input type="button" value="submit" onclick="getFields();">
</div>
    </html>
    
    <javascript>
      var num_q=document.getElementById('numquest').value;
     //code for dynamic creation
    </javascript>

推荐答案

尝试一下:

使用jQuery:

您可以使用以下方法重置整个表单:

You can reset the entire form with:

$("#myform")[0].reset();

或仅使用以下特定字段:

Or just the specific field with:

$('#form-id').children('input').val('')

在不使用jQuery的情况下使用JavaScript

<input type="button" value="Submit" id="btnsubmit" onclick="submitForm()">

function submitForm() {
   // Get the first form with the name
   // Hopefully there is only one, but there are more, select the correct index
   var frm = document.getElementsByName('contact-form')[0];
   frm.submit(); // Submit
   frm.reset();  // Reset
   return false; // Prevent page refresh
}

这篇关于提交表单后如何清除上一个文本字段的值,而不刷新整个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆