提交表单时清除特定字段(不是全部)

时间:2017-08-30 05:29:57

标签: javascript

我试图仅清除密码并确认密码字段不匹配时。 我按下提交按钮时调用一个函数,其中包含一个密码检查条件。如果条件为真,我应该如何仅清除密码和确认密码输入?

function checkmatch()
    {   
        var name = $("#username").val();
        var address = $("#address").val();
        var email = $("#emailid").val();
        var mobile = $("#phonenum").val();
        var password = $("#newPwd").val();
        var cnfirmPassword = $("#confirmPwd").val();

        if(password != cnfirmPassword)
        {
            alert("Passwords do not match.");
            return false;

        }
        else if((name==null || name == "") || (address==null || address == "") || (email==null || email == "") || (mobile=null || mobile == "") || (password==null || password == "") || (cnfirmPassword==null || cnfirmPassword == ""))
        {
            alert("Please fill all the required fields.");
            return false;
        }
        else
        {
            $.ajax(
                {
                    type: "POST",
                    url: "assmt1.php",
                    datatype: "html",
                    data:  $("#fm1").serialize(),
                    success: function(response)
                    {

                    }
                });
            alert("Your form has been submitted. Thank you!");  
        }               
    }

2 个答案:

答案 0 :(得分:1)

您需要做的就是调用与获取值相同的函数。

只需将空字符串传递给(myProperty == "value1" OR myProperty == "value2") AND myProperty != "0" ,如下所示:

val()

答案 1 :(得分:1)

这是你的意思吗?

    var password = $("#newPwd").val();
    var cnfirmPassword = $("#confirmPwd").val();

    if(password != cnfirmPassword)
    {
        alert("Passwords do not match.");
        $("#newPwd").val('').focus(); ///this will not focus because of alert
        $("#confirmPwd").val('');
        return false;

    }