只有在javascript被禁用的情况下,才能启用提交事件/提交按钮 [英] Supress submit event / Submit button enabled only if javascript is disabled

查看:127
本文介绍了只有在javascript被禁用的情况下,才能启用提交事件/提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一些客户端JavaScript验证。它效果很好。但我想适应JavaScript禁用的用户。我的验证不是从窗体的onsubmit属性运行的,它是绑定到窗体中的普通按钮的事件处理程序。因此,启动验证并提交的按钮实际上不是提交,它只是type =button:

I have some client-side JavaScript validation on a form. It works great. But I want to accommodate users who have JavaScript disabled. My validation isn't run from the form's onsubmit attribute, it's an event handler that is bound to a normal button in the form. So the button that kicks off the validation and submit isn't actually a submit, it's just type="button":

<input type="button" value="Ok" class="okbtn">

然后我注册一个事件处理程序到它的click事件中,这个事件处理我的验证。它会提交一切是否通过:

Then I register an event handler to its click event that does my validation. It submits if everything passes:

function clickHandler(event){
    thisForm = event.data.caller
    var valid = submitValidation(thisForm);
    if (valid == true){
        thisForm.submit();
    } else{
      }
}

我的问题是if用户有JS禁用,无法提交表单。我可以让按钮 type =submit,这对JS禁用的用户来说很有效,但是它会一直提交表单并绕过JS的验证启用用户。 (验证会运行,但它会提交)。如果我点击按钮 type =submit,我可以阻止提交事件吗?我正在使用JQuery,可以JQuery抑制提交?我希望我的验证能够处理它是否被提交。

My problem is if the user has JS disabled, there is no way to submit the form. I could make the button type="submit", which would work for the JS-disabled users, but then it will *always submit the form and bypass the validation for the JS-enabled users. (The validation will run but it will submit anyway). If I make the button type="submit" can I block the submit event if it's clicked? I'm using JQuery, can JQuery suppress the submit? I want my validation to handle whether it gets submitted.

推荐答案

您可以通过执行以下操作来禁止提交点击:

You can suppress submit clicks by doing something like:

$('input[type=submit]').bind('click', function(e) {
    e.preventDefault() // prevents the form from being submitted
    clickHandler(); // the custom submit action
});

或者,您可以禁止实际的表单提交,如下所示:

Or, you can suppress the actual form submit like this:

$('form').bind('submit', function(e) {
    e.preventDefault();
    // etc
});

这篇关于只有在javascript被禁用的情况下,才能启用提交事件/提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆