简体   繁体   English

表单onsubmit不执行功能

[英]form onsubmit doesn't execute function

I've created this simple form setup, where I want a validator to check a field before accepting the form. 我创建了这个简单的表单设置,我希望验证器在接受表单之前检查字段。 But whether I use buttons or input, I can't seem to make it call my validateForm() function. 但无论我使用按钮还是输入,我似乎无法调用我的validateForm()函数。 At one point I got it working, but after uploading it to my server, then pulling it using git, it completely stopped calling the function again. 有一次我让它工作,但在上传到我的服务器,然后使用git拉它,它完全停止再次调用该功能。

I am severely confused, as to my eyes, this should work. 我非常困惑,至于我的眼睛,这应该有效。 Are there any alternatives, solutions or workaround for this issue? 针对此问题是否有任何替代方案,解决方案或解决方法?

 function validateForm(e) { if (document.reg_form.reg_code.value == "unique code value") { return true; } swal({ type: 'error', title: 'Du har ikke indtastet en gyldig registreringskode.', showCancelButton: false, confirmButtonText: 'OK', confirmButtonColor: '#00a651', }); e.preventDefault(); return false; } 
 <form action="<?= get_home_url(); ?>/create-profile/" method="post" onsubmit="validateForm(event)" name="reg_form" id="reg_form" class="form"> <div class="form__field"> <label for="" class="form__label">Registreringskode</label> <input type="text" class="form__input" required placeholder="Indtast din tilsendte kode" name="user[reg_code]" id="registration" /> <p class="hidden" id="code_warning">Den indsatte kampagnekode findes ikke!</p> </div> <input type="submit" value="Opret bruger" form="reg_form" class="btn btn--green btn--large btn--block"> </form> 

Seems like you are not returning anything onsubmit . 好像你没有在onsubmit返回任何内容。

It should be onsubmit="return validateForm(event)" 它应该是onsubmit="return validateForm(event)"

Try 尝试

<form action="<?= get_home_url(); ?>/create-profile/" method="post" onsubmit="return validateForm(event)" name="reg_form" id="reg_form" class="form">

...
</form>

Update: I just noticed you are accessing the form input wrong and is throwing an error in the console. 更新:我刚刚注意到您正在访问表单输入错误并在控制台中抛出错误。

function validateForm(e) {
  if (document.reg_form['user[reg_code]'].value == "unique code value") {
}

A better approach would be to access the input directly using its id. 更好的方法是使用其id直接访问输入。

 function validateForm(e) { if (document.getElementById('registration').value == "unique code value") { return true; } alert("Validation failed"); e.preventDefault(); return false; } 
 <form action="" method="post" onsubmit="return validateForm(event)" name="reg_form" id="reg_form" class="form"> <div class="form__field"> <label for="" class="form__label">Registreringskode</label> <input type="text" class="form__input" required placeholder="Indtast din tilsendte kode" name="user[reg_code]" id="registration" /> <p class="hidden" id="code_warning">Den indsatte kampagnekode findes ikke!</p> </div> <input type="submit" value="Opret bruger" form="reg_form" class="btn btn--green btn--large btn--block"> </form> 

Robin C Samuel is right, only suspicious thing on that code is missing return. 罗宾C塞缪尔是对的,唯一可疑的是该代码缺少回报。 However, as you talk about uploading to server and checking out ... are you sure you didn't forgot to add some file into git? 但是,当你谈到上传到服务器并检出...你确定你没有忘记在git中添加一些文件吗? Either the file with the validateForm may be missing, or the file with the swal function (if there is error in submit handler, like missing function, javascript will fail and browser will then submit the form.) 可能缺少带有validateForm的文件,或者带有swal函数的文件(如果提交处理程序中有错误,如缺少函数,javascript将失败,浏览器将提交表单。)

... wait. ......等一下 One more thing. 还有一件事。 Try 尝试

swal({
    type: 'error',
    title: 'Du har ikke indtastet en gyldig registreringskode.',
    showCancelButton: false,
    confirmButtonText: 'OK',
    confirmButtonColor: '#00a651'
  });
  • note the missing comma after last field. 注意最后一个字段后丢失的逗号。

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

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