代码的相关部分如下所示。当我单击“提交”按钮时,在警报框中,我得到的是不确定的,而不是键入的电子邮件。我在这里犯什么错误?

<form action="page2.php" method="post" onsubmit="myFunction()" id="form1">
        ......
        <input type="text" name="YourEMail" style="width: 250px;">
        <input type="submit" name="submit" value="Submit"> <br>
        ......
</form>

<script>
$('#form1').submit(function() {
    //your validation rules here
    var email = $('#YourEMail').val()
    alert(email)
    //if(email.length == 0)
        return false;
    //else
    //  return true;
});
</script>

最佳答案

YourEMailname,而不是id:

var email = $('input[name=YourEMail]').val();

或将input更改为id:
<input type="text" id="YourEMail" name="YourEMail" style="width: 250px;">

09-25 18:36