简体   繁体   English

如何重置新添加的表单输入

[英]How to Reset Newly Added Form Inputs

edits: 编辑:
1. removed code that was unnecessary... 1.删​​除了不必要的代码...
2. Clarified desired solution and added reset button HTML. 2.阐明了所需的解决方案,并添加了重置按钮HTML。

I've seen various methods used to reset a form but I add content to a blank div (id = "unitPulled") via AJAX so, methods similar to the following will not work: 我已经看到了用于重置表单的各种方法,但是我通过AJAX将内容添加到空白div(id =“ unitPulled”)中,因此类似于以下内容的方法将不起作用:

document.forms[0].reset();

My goal is to reset the entire form when a user hits a reset button. 我的目标是当用户点击重置按钮时重置整个表单。

How do I go about tackling this issue? 我该如何解决这个问题? Would the DOM have to be reloaded? DOM是否必须重新加载?

HTML for button: 按钮的HTML:

<button id="reset" name="reset" class="btn btn-default" type="reset">Clear</button>

Javascript: Javascript:

/* Below is for auto-updating of initial and number */
var oXmlHttp

function showUnits(str) // Change function name
{
    var url = "ajax.cfm?&lic_plate=" + str // Change URL
    oXmlHttp = GetHttpObject(stateChanged)
    oXmlHttp.open("GET", url, true)
    oXmlHttp.send(null)
}

// Below is where new elements are added:
function stateChanged() {
    if (oXmlHttp.readyState == 4 || oXmlHttp.readyState == "complete") {
       document.getElementById("unitPulled").innerHTML = oXmlHttp.responseText; // Change ID
       document.getElementById("lic_plate").focus();

    }
}

Thanks! 谢谢!

Add this line in you function stateChanged() function stateChanged()添加此行

function stateChanged() {
    if (oXmlHttp.readyState == 4 || oXmlHttp.readyState == "complete") {
       document.getElementById("unitPulled").innerHTML = oXmlHttp.responseText; // Change ID
       document.getElementById("myForm").reset(); 
       document.getElementById("lic_plate").focus();
    }
}

Please put your form id instead of myForm. 请输入您的表单ID而不是myForm。 Let me know if this is not working as form value will be reset after ajax call. 让我知道这是否不起作用,因为ajax调用后将重置表单值。

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

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