onsubmit 函数未使用 Node.js 正确调用
onsubmit function does not invoke correctly using Node.js
我想在提交表单时进行一些验证。但是,当提交表单时,验证功能似乎正确调用了。警报部分没有出现,但是当我尝试像这样警报时
function validation(){
alert("somethinglol");
var username = $("#register.username").val().toString();
alert(username);
};
somethinglol 出现,但第二个警报没有出现。
这是我的获取函数:
app.get('/', (req, res) => {
res.render('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>
这是 index.js 的脚本部分,其中包含 validation() 函数。
<script>
function validation(){
alert($("#register.username").val().toString());
var username = $("#register.username").val().toString();
username = "somethinglol";
alert(username);
};
</script>
我已将脚本更改为外部脚本,但没有用。我已经更改了 jquery cdn。我使用 $("#registerSubmit").click(...)
而不是 onSubmit 但它再次不起作用。我也尝试了一些使用 async 和 await 的东西,但如果这是问题所在,我不确定我是否正确地做了。
使用 jquery 您可以使用 $( "#form1" ).submit(function( event ) {
在提交前执行操作
此外 select 在 jquery 中使用点 (.) 的 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>
我想在提交表单时进行一些验证。但是,当提交表单时,验证功能似乎正确调用了。警报部分没有出现,但是当我尝试像这样警报时
function validation(){
alert("somethinglol");
var username = $("#register.username").val().toString();
alert(username);
};
somethinglol 出现,但第二个警报没有出现。
这是我的获取函数:
app.get('/', (req, res) => {
res.render('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>
这是 index.js 的脚本部分,其中包含 validation() 函数。
<script>
function validation(){
alert($("#register.username").val().toString());
var username = $("#register.username").val().toString();
username = "somethinglol";
alert(username);
};
</script>
我已将脚本更改为外部脚本,但没有用。我已经更改了 jquery cdn。我使用 $("#registerSubmit").click(...)
而不是 onSubmit 但它再次不起作用。我也尝试了一些使用 async 和 await 的东西,但如果这是问题所在,我不确定我是否正确地做了。
使用 jquery 您可以使用 $( "#form1" ).submit(function( event ) {
在提交前执行操作
此外 select 在 jquery 中使用点 (.) 的 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>