Thesubmit() function is used to bind a handler function to the submit event of each matching element. This function can also be used to trigger the submit event. In addition, you can also pass some additional data to the Event Handling function.
The submit event will be triggered when the form is submitted. This event only applies to
$("form").submit( function(event){ if( !$("#name").val() ){ alert("姓名不能为空!"); return false; // 返回值为false,将阻止表单提交 }else if( !$("#age").val() ){ alert("年龄不能为空!"); return false; // 返回值为false,将阻止表单提交 } } ); // 触发form元素的submit事件 // $("form").submit( );
We can also pass some additional data to the event processing function. In addition, through the parameter Event object passed in by jQuery for the event processing function, we can obtain relevant information about the current event (such as event type, DOM element that triggered the event, additional data, etc.):
var map = { name: "姓名", age: "年龄" }; $("form").submit( map, function(event){ var labelMap = event.data; var label = ''; // 循环验证所有text元素是否为空 $(this).find(":text").each(function(){ if( !this.value ){ label = labelMap[this.name]; return false; } }); if( label ){ alert( label + "不能为空!" ); return false; } } );
The above is the detailed content of Detailed explanation of jQuery.submit() function. For more information, please follow other related articles on the PHP Chinese website!