简体   繁体   English

onsubmit function 未使用 Node.js 正确调用

[英]onsubmit function does not invoke correctly using Node.js

I want to make some validations when the form submitted.我想在提交表单时进行一些验证。 However when the form submitted validation function seems like invoking correctly.但是,当表单提交验证 function 似乎正确调用时。 The alert part doesn't come up but when i try to alert like this警报部分没有出现但是当我尝试像这样发出警报时

function validation(){
      alert("somethinglol");
      var username = $("#register.username").val().toString();
     
       
       
       alert(username);
     };

somethinglol appears but the second alert doesn't. somethinglol 出现,但第二个警报没有出现。

This is my get function:这是我得到的 function:

app.get('/', (req, res) => {
    res.render('index.ejs');
})

This is the index.ejs这是 index.ejs

<form id="form1" action="/" method="post" onSubmit="validation();">
      <div class="form-group">
        <label for="register.username">Username</label>
        <input type="text" name="username" id="register.username" class="form-control" placeholder="Username" aria-describedby="helpId">
        <small id="helpRegisterUsername" class="text-muted">You should enter a username.</small>
      </div>
      <div class="form-group">
        <label for="register.password">Password</label>
        <input type="password" name="password" id="register.password" class="form-control" placeholder="Password" aria-describedby="helpId">
        <small id="helpId" class="text-muted">Enter a password.</small>
      </div>
      <div class="form-group">
        <label for="register.confirm.password">Confirm password</label>
        <input type="password" name="confirmedPassword" id="register.confirm.password" class="form-control" placeholder="Confirm password" aria-describedby="helpId">
        <small id="helpId" class="text-muted">Confirm password</small>
      </div>
      <input type="submit" id="registerSubmit" value="Submit"></input>

    </form>

This is the script part of index.js which contains the validation() function.这是 index.js 的脚本部分,其中包含 validation() function。

<script>
     function validation(){
      alert($("#register.username").val().toString());
      var username = $("#register.username").val().toString();
      username = "somethinglol";
       
       
       alert(username);
     };
</script>

I have changed the script as external but it didn't work.我已将脚本更改为外部脚本,但没有用。 I have changed the jquery cdn.我已经更改了 jquery cdn。 I used $("#registerSubmit").click(...) instead of onSubmit but it didn't work again.我使用$("#registerSubmit").click(...)而不是 onSubmit 但它没有再次工作。 I also tried something using async and await but if it's the problem I am not sure that I did it correctly.我还尝试了一些使用 async 和 await 的方法,但如果这是问题所在,我不确定我是否正确地做了。

with jquery you can use $( "#form1" ).submit(function( event ) { to perform an action before the submit对于 jquery,您可以使用$( "#form1" ).submit(function( event ) {在提交之前执行操作

moreover to select id with dot (.) in jquery you have to use \\ before dot此外,在 jquery 中带有点 (.) 的 select id 你必须在点之前使用 \\

$("#register\\.username")

 $( "#form1" ).submit(function( event ) { alert($("#register\\.username").val().toString()); var username = $("#register\\.username").val().toString(); username = "somethinglol"; alert(username); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="form1" action="/" method="post" onSubmit="validation();"> <div class="form-group"> <label for="register.username">Username</label> <input type="text" name="username" id="register.username" class="form-control" placeholder="Username" aria-describedby="helpId"> <small id="helpRegisterUsername" class="text-muted">You should enter a username.</small> </div> <div class="form-group"> <label for="register.password">Password</label> <input type="password" name="password" id="register.password" class="form-control" placeholder="Password" aria-describedby="helpId"> <small id="helpId" class="text-muted">Enter a password.</small> </div> <div class="form-group"> <label for="register.confirm.password">Confirm password</label> <input type="password" name="confirmedPassword" id="register.confirm.password" class="form-control" placeholder="Confirm password" aria-describedby="helpId"> <small id="helpId" class="text-muted">Confirm password</small> </div> <input type="submit" id="registerSubmit" value="Submit"></input> </form>

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

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