【问题标题】:clear input fields after submit form提交表单后清除输入字段
【发布时间】:2016-08-01 08:34:16
【问题描述】:

我正在尝试在提交表单后清除输入字段,但它只是清除文本,但我需要该边框,并且某些属性也将是

//验证名

document.questionform.name.onchange= function() {
            var name = document.questionform.name.value;
            if (name === "") {
                document.questionform.name.removeAttribute("class", "ready");
                document.questionform.name.style.border = "1px solid #da3637";
                document.getElementById("questionError").style.display = "block";
                document.getElementById("questionErrorTwo").style.display = "none";
            } else {
                    document.questionform.name.style.border = "1px solid #509d12";
                    document.getElementById("questionError").style.display = "none";
                    var pattern = new RegExp("^[а-я]+$", "i");
                    var isValid = this.value.search(pattern) >= 0;
                    if (!(isValid)) {
                        document.questionform.name.style.border = "1px solid #da3637";
                        document.getElementById("questionErrorTwo").style.display = "block";
                        document.questionform.name.removeAttribute("class", "ready");
                    } else {
                        document.getElementById("questionErrorTwo").style.display = "none";
                        document.questionform.name.style.border = "1px solid #509d12";
                        document.questionform.name.setAttribute("class", "ready");
                    }
            }
        };

阿贾克斯: $("#questionsave").click(function() {

    var url = "questionForm.php"; 

    $.ajax({
           type: "POST",
           url: url,
           data: $("#questform").serialize(), 
           success: function(data)
           {
                var name = $('input[name=name]').val("");
                var tel = $('input[name=phone]').val("");
                document.questionform.phone.removeAttribute("class", "ready");
                document.questionform.name.removeAttribute("class", "ready");
                document.getElementById("questionsave").setAttribute("disabled");
                document.getElementById("questionsave").style.cursor = "not-allowed";
                document.getElementById("questionsave").style.opacity = "0.8";
                document.questionform.name.style.borderColor = "#e4e4e6";
                document.questionform.phone.style.borderColor = "#e4e4e6";
           }
         });

    return false; 
    });

【问题讨论】:

  • 重新加载页面?提交后?
  • 是的,提交后,在我的代码中它只清除文本
  • 以某种方式突出显示您所说的确切执行文本不会被删除,以防此问题仍然存在

标签: javascript jquery forms


【解决方案1】:

我不确定你在尝试什么。表单提交后,表单已经重置。当您使用 ajax 时,它会保留其价值。在这种情况下,试试这个:

$('#form_id')[0].reset();

【讨论】:

    【解决方案2】:

    为您的form 提供id 并使用它,

    document.getElementById("formId").reset();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 2023-03-09
      • 2020-09-11
      • 2016-10-23
      • 1970-01-01
      相关资源
      最近更新 更多