简体   繁体   English

使用jQuery .on()方法进行按钮单击事件而不是表单提交事件时,html5表单将未经验证而提交。 为什么?

[英]html5 form get submitted without validation when using jQuery .on() method for button - click event rather than form - submit event. why?

Below code makes form to be submitted without html5 validation... 下面的代码使表单无需html5验证即可提交...

$j(document).on("click", "#client_entry_add", function(event){ ajax_submit();});

While below code lets html5 validation happening BUT in opera browser it also doesn't work and get submitted without validation(in other browsers(checked for firefox/chrome) validation happens) 虽然下面的代码允许html5验证在Opera浏览器中发生,但它也无法正常工作,并且未经验证就提交(在其他浏览器中(检查过Firefox / chrome)验证发生了)

$j(document).on("submit", "#client_entry_form", function(event){ ajax_submit();});

my questions are 我的问题是

  1. why this happens when we bind handler to click event instead of submit ? 当我们将处理程序绑定到click事件而不是Submit时,为什么会发生这种情况?
  2. why even not working with submit event type in opera? 为什么甚至不使用Opera中的Submit事件类型?

thank you. 谢谢。

================================================================================== ================================================== ================================

form and javascript code 表格和JavaScript代码

function ajax_CALL(OBJECT , URL , PARAMS , TARGET)
{//alert ('in for -> ' + URL);
if (typeof OBJECT != 'undefined' && OBJECT != null )
{   
    if( $j('#'+OBJECT).length ){OBJECT = $j('#'+OBJECT);}
    else
    if(  $j('.'+OBJECT).length ){OBJECT = $j('.'+OBJECT);}

    var ObjName  = OBJECT.attr("name");
    var ObjValue  = OBJECT.val();

    var ob_defined = true;
}

if ((typeof PARAMS == 'undefined' || PARAMS == null) && ob_defined)
{
    var PARAMS = ObjName+'='+ObjValue;// set params to pass via ajax call
}


$j.ajax({
   type: "POST",
   url: URL,
   data: PARAMS,  //"name=John&location=Boston",$j('#ContactForm').serialize()
   success: function(responseText){
//////////////////////////////////////
//hide the progress bar
//////////////////////////////////////
$j('#loading').hide();   
//////////////////////////////////////
if (typeof TARGET != 'undefined' )
{
if($j('#'+TARGET).length){TARGET = $j('#'+TARGET);}//if id
else
if($j('.'+TARGET).length){TARGET = $j('.'+TARGET);}//if class
                                            TARGET.html(responseText);
                                            //show TARGET div and display the content with fadeIn transition
                                            TARGET.fadeIn(250);
                                            //$j(TARGET).html(responseText);
                                            //display the body with fadeIn transition
                                            //$j(TARGET).fadeIn(250);       
           }
        }
    }); 
}
/*********************************************************/
$j(document).ready(function(){

    //$j(document).on("click", "#client_entry", function(event)
$j(document).on("submit", "#client_entry_form", function(event){
    //alert('ohh hhh yes :)');return false;
    //prevent default
    event.preventDefault();

    //hide the middle content and show the loading progress animation
    show_hide();

    var OBJECT = null;
    var URL = $j('#client_entry_form').attr('action');
    var PARAMS = $j('#client_entry_form').serialize();
    var TARGET = 'middle_content';
///////////////////////////////////////////////////////////////////////////////////////////
    //run ajax
    ajax_CALL(OBJECT , URL , PARAMS , TARGET);      

    //cancel the anchor tag behaviour or any other default event/trigger
    if(!event.isDefaultPrevented())
    return false;

    });
})
======================================
html5 form
<div id="client_entry_form_div">
<form action="client_entry_action.php" method="post" id="client_entry_form" name="client_entry_form" >

<fieldset id="client_info_1">   

    <label for="c_name">Name:</label> 
    <input type="text" name="c_name" required placeholder="Name" autofocus="autofocus" />

    <label for="c_phone">Phone Number:</label> 
    <input type="tel" name="c_phone" required placeholder="Mobile/Phone Number" />

    <label for="c_email">Email:</label> 
    <input type="email" name="c_email" placeholder="email@example.com" />

    <label for="c_address">Address:</label> 
    <textarea name="c_address" ></textarea>

</fieldset>

<fieldset id="client_info_2">   

    <label for="c_info">Additional notes:</label> 
    <textarea name="c_info" ></textarea>

    <input type="hidden" name="client_entry" value="add" />
    <input type="submit" name="client_entry_add" value="Add Client" id="client_entry_add" />

</fieldset>
</form>
</div>

In the first version, submission is entirely synthetic; 在第一个版本中,提交完全是合成的。 HTML's natural form submission process is suppressed and everything that occurs is performed by javascript/jQuery. HTML的自然表单提交过程受到抑制,发生的一切都由javascript / jQuery执行。

In the second version, HTML's natural form submission process is allowed to initiate but is then intercepted by javascript/jQuery in the guise an "onsubmit" handler. 在第二个版本中,HTML的自然表单提交过程被允许启动,但随后被javascript / jQuery以“ onsubmit”处理程序的形式拦截。

HTML5 form validation is, understandably, part of the natural HTML process and (I didn't know this before) must occur prior to the "onsubmit" event. 可以理解,HTML5表单验证是自然HTML流程的一部分,并且(我以前不知道)必须在“ onsubmit”事件之前进行。

EDIT: 编辑:

I can only assume that the HTML5 standard does not specify the order of events in detail and (from what you say) Opera's implementation is different in this regard from the other (validation-capable) browsers. 我只能假设HTML5标准没有详细说明事件的顺序,并且(根据您的说法)Opera的实现在这方面不同于其他(具有验证功能的)浏览器。

Out of interest, the accepted answer to this question tells us that "you can't trigger the native validation UI, but you can easily take advantage of the validation API on arbitrary input elements". 出于兴趣, 对此问题的公认答案告诉我们:“您无法触发本机验证UI,但可以轻松地在任意输入元素上利用验证API”。 This offers the possibility of a workaround for Opera's shortcoming, by using your first approach and triggering validation on each form field individually. 通过使用第一种方法并分别触发每个表单字段上的验证,这提供了一种解决Opera缺点的方法。 But hopefully Opera will fix the problem so this is not necessary in the longer term. 但是希望Opera可以解决此问题,因此从长远来看这不是必需的。

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

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