[英]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();});
我的问题是
谢谢。
================================================== ================================
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.