使标签像输入按钮一样
如何使Test
像表单按钮一样? 通过表现forms按钮,我的意思是当点击链接做一个method="get"
或发布,以便能够通过获取或发布捕获它。
没有必要是一个链接,我可以适应,以使它像这样工作!
如果您想使用链接提交表单:
HTML –
SUBMIT
JS –
$(function () { $('#form-submit').on('click', function () { //fire the submit event on the form $('#my-form').trigger('submit'); //stop the default behavior of the link return false; }); });
trigger()
文档trigger()
: http : //api.jquery.com/trigger
如果您想在不离开页面的情况下提交表单,可以使用AJAX调用:
$(function () { $('#form-submit').on('click', function () { //cache the form element for use later var $form = $('#my-form'); $.ajax({ url : $form.attr('action') || '',//set the action of the AJAX request type : $form.attr('method') || 'get',//set the method of the AJAX reqeuest data : $form.serialize(), success : function (serverResponse) { //you can do what you want now, the form has been submitted, and you have received the serverResponse alert('Form Submitted!'); } }); }); $('#my-form').on('submit', function () { //stop the normal submission of the form, for instance if someone presses the enter key inside a text input return false; }); });
$.ajax()
文档: http : //api.jquery.com/jquery.ajax
请注意.on()
是jQuery 1.7中的新增内容,在这种情况下与使用.bind()
相同。
假设您的表单中有任何带有ID的元素,您可以使用jQuery选择该ID并在其上附加click
事件。 在这种特殊情况下,它还将使用get
来请求来自/whatever.php
数据,您应该对其进行微调,以便根据您的需要使用get/post
和序列化表单数据。
$("#whatever").click(function(){ $.get("/whatever.php"); });
没有jQuery