jquery点击()在提交按钮 [英] jquery click() in submit button

查看:106
本文介绍了jquery点击()在提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上的提交按钮上有一个.click()函数:

I have a .click() function on a submit button in a form:

$("#submitId").click(function () {
    $('#hiddenInput').val(someVariable);
});

它的作用就像一个魅力。当点击提交按钮时,click()函数填充隐藏的输入变量,然后提交整个表单。然后服务器接收带有刷新内容的隐藏输入。

It works like a charm. When the submit button is clicked the click() function fills the hidden input variable and then the whole form gets submitted. The server then receives the hidden input with the refreshed content.

我的问题是:它会一直工作吗?有没有任何危险,由于某种原因不知道我,提交操作首先执行和click()函数后?

My question is: will it always work? Is there any danger that, by some reason not yet known to me, the submit operation gets executed first and the click() function later? I want to make sure the hidden input always gets refreshed.

推荐答案

使用表单时,总是建议让提交按钮做它的工作:触发表单的提交事件 ...这就是它的意思。然后,您将在表单上听提交事件,而不是点击提交按钮。

Whenever working with forms, it's always advisable to let the submit button do it's job: TRIGGER THE FORM'S SUBMIT EVENT ... that's what it's meant for. Then you would listen for the submit event on the form rather than the click event on the submit button.

您可以使用 event.preventDefault ()以防止提交表单,以便您可以进行保存,那么您可以提交表单。

You can use event.preventDefault() to prevent submission of the form so that you can do some house keeping then you can submit the form.

$("#submitId").closest('form').on('submit', function(e) {
    e.preventDefault();
    $('#hiddenInput').val(someVariable); //perform some operations
    this.submit(); //now submit the form
});

或者简单地,

$('form').on('submit', function(e) {
    e.preventDefault();
    $('#hiddenInput').val(someVariable); //perform some operations
    this.submit(); //now submit the form
});

这篇关于jquery点击()在提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆