Ale*_*lex 3 javascript jquery post get hyperlink
我怎样才能<a href="http://test/com/tag/test">Test</a>像表单按钮那样表现?通过表现形式按钮,我的意思是当点击链接做一个method="get"或发布,以便能够通过获取或发布捕获它.
没有必要成为一个链接,我可以适应,以使其像这样工作!
如果您想使用链接提交表单:
HTML -
<form action="my-page.php" id="my-form" method="post">...</form>
<a href="#" id="form-submit">SUBMIT</a>
Run Code Online (Sandbox Code Playgroud)
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;
});
});
Run Code Online (Sandbox Code Playgroud)
文档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;
});
});
Run Code Online (Sandbox Code Playgroud)
文档$.ajax():http://api.jquery.com/jquery.ajax
请注意,这.on()是jQuery 1.7中的新增内容,在这种情况下与使用相同.bind().
| 归档时间: |
|
| 查看次数: |
3255 次 |
| 最近记录: |