簡體   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?

下面的代碼使表單無需html5驗證即可提交...

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

雖然下面的代碼允許html5驗證在Opera瀏覽器中發生,但它也無法正常工作,並且未經驗證就提交(在其他瀏覽器中(檢查過Firefox / chrome)驗證發生了)

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

我的問題是

  1. 當我們將處理程序綁定到click事件而不是Submit時,為什么會發生這種情況?
  2. 為什么甚至不使用Opera中的Submit事件類型?

謝謝。

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

表格和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>

在第一個版本中,提交完全是合成的。 HTML的自然表單提交過程受到抑制,發生的一切都由javascript / jQuery執行。

在第二個版本中,HTML的自然表單提交過程被允許啟動,但隨后被javascript / jQuery以“ onsubmit”處理程序的形式攔截。

可以理解,HTML5表單驗證是自然HTML流程的一部分,並且(我以前不知道)必須在“ onsubmit”事件之前進行。

編輯:

我只能假設HTML5標准沒有詳細說明事件的順序,並且(根據您的說法)Opera的實現在這方面不同於其他(具有驗證功能的)瀏覽器。

出於興趣, 對此問題的公認答案告訴我們:“您無法觸發本機驗證UI,但可以輕松地在任意輸入元素上利用驗證API”。 通過使用第一種方法並分別觸發每個表單字段上的驗證,這提供了一種解決Opera缺點的方法。 但是希望Opera可以解決此問題,因此從長遠來看這不是必需的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM