jQuery 在jQuery提交中设置提交值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3531505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:36:17  来源:igfitidea点击:

Setting submit value in jQuery submit

jqueryforms

提问by mcgrailm

I want to use jQuery to submit my form and i can do that like so

我想使用 jQuery 提交我的表单,我可以这样做

  $('#myform_id').submit();

and thats all good but normally someone clicks a submit button than that submit button has a value so when I do the submit via jQuery I also need to set a submit value how do i do that ?

这一切都很好,但通常有人点击提交按钮而不是提交按钮有一个值,所以当我通过 jQuery 提交时,我还需要设置一个提交值,我该怎么做?

EDIT

编辑

as noted by some answers I could do this by simulating a user click but I don't want to do that I want to pass the submit value when I use the .submit() command if that is possible

正如一些答案所指出的,我可以通过模拟用户点击来做到这一点,但我不想这样做,如果可能的话,我想在使用 .submit() 命令时传递提交值

ultimately the submit using ajax form

最终使用ajax表单提交

** Clarifying **

** 澄清 **

ok so I have jQuery setup to use jquery validate and has a submit handler like so

好的,所以我有 jQuery 设置来使用 jquery 验证并有一个像这样的提交处理程序

$("#myform_id").validate({
    submitHandler: function() {
        run_ajax(obj);
    }
});

then that calls I function i call run_ajax passing and object containing information about what type of ajax i want and what have you.

然后调用 I 函数,我调用 run_ajax 传递和对象,其中包含有关我想要什么类型的 ajax 以及你有什么的信息。

and ultimately runs this

并最终运行这个

obj.formitem.ajaxSubmit({
    dataType:'json',
    data: obj.pdata,
});

where

在哪里

  obj.formitem == $('#myform_id');

so when the form is submit all that happens all good.

所以当表单提交时,一切都很好。

so I load the page with this form into jquery dialog and I want them to be able to click a button in the dialog that triggers the form submit and also tells the form what type of submit was requested

所以我将带有这个表单的页面加载到 jquery 对话框中,我希望他们能够点击对话框中触发表单提交的按钮,并告诉表单请求的提交类型

hopefully this is clear now sorry for confusion

希望这很清楚了,抱歉混淆

采纳答案by sjobe

One quick way would be $('#myform_id #submitButtonID').click();

一种快速的方法是 $('#myform_id #submitButtonID').click();

回答by brian_d

https://stackoverflow.com/a/993897/215887worked for me

https://stackoverflow.com/a/993897/215887为我工作

Well actually I did it this way:

好吧,实际上我是这样做的:

$('<input />').attr('type', 'hidden')
              .attr('name', 'myButton')
              .attr('value', 'MyButton')
              .appendTo('#my-form');
$('#my-form').submit();

回答by DMin

.submit()will get you the traditional submit that happens POST/GET with the page refresh.

.submit()将使您在页面刷新时进行 POST/GET 的传统提交。

For ajax, you will need to use either $.post, $.get or $.ajax -- eg:

对于 ajax,您需要使用 $.post、$.get 或 $.ajax —— 例如:

var aaa = $("#your_form").serialize();
$.post("your_url",aaa,function(response){/*do something*/},"json");

As far as clicking of buttons is concerned, if you have multiple buttons that can possibly submit one form. You can use hidden fields as part of the form. Before you submit the form, change the hidden field according to the button that was pressed.

就单击按钮而言,如果您有多个按钮可以提交一个表单。您可以使用隐藏字段作为表单的一部分。在提交表单之前,根据按下的按钮更改隐藏字段。

$(".submit_buttons").click(function(){

$("#my_hidden_input").val($(this).attr("rel"));

/* 
above serialize and ajax post code
*/

});

HTML :

HTML :

<input type="button" class="submit_buttons" rel="special_button1" value="Button 1"/>
<input type="hidden" id="my_hidden_input" />


When I use this validation plugin, This is how I go about it.

当我使用这个验证插件时,我就是这样做的。

$("#right_form").validate(); //To add validation to the form

validate9 = $("#right_form").validate().form(); //Check if the form validates 

Then use ifto check validate9 is trueand submit using $.post().

然后用于if检查 validate9 是true并使用 提交$.post()

Normal ajax is pretty straightforward.

普通的 ajax 非常简单。

A few reasons why I think you probably are using iframe to submit the form :
1) Its has to be done this way(some unchangeable requirement).
2) You want to upload some files using ajax.

我认为您可能正在使用 iframe 提交表单的几个原因:
1)必须以这种方式完成(某些不可更改的要求)。
2)您想使用ajax上传一些文件。

Ref: JS iframe limitations.

参考:JS iframe 限制。

Not sure if this helps, but, if you figure this out please post an answer to let us know how you took care of this.

不确定这是否有帮助,但是,如果您解决了这个问题,请发布答案,让我们知道您是如何处理这个问题的。

回答by schettino72

   $('#myform').submit(function(event){
      /* stop form from submitting normally */
      event.preventDefault();

      // get form data and include value from submit button
      var post_data = form.serializeArray();
      post_data.push({name: event.originalEvent.explicitOriginalTarget.name,
                      value: event.originalEvent.explicitOriginalTarget.value
                     });

      // ...
   }

回答by mbrevoort

The submit value is just passed as a parameter. So if you have:

提交值只是作为参数传递。所以如果你有:

<input type="submit" name="mysubmit" value="click"/>

then the request will contain a request parameter mysubmit=click .

那么请求将包含一个请求参数 mysubmit=click 。

So just add a hidden input type like this:

所以只需添加一个像这样的隐藏输入类型:

<input type="hidden" name="mysubmit" value="click"/>

回答by 3rgo

This should do the trick :

这应该可以解决问题:

$('#form_id #submitbutton_id').val('your value');

回答by Teja Kantamneni

You can add a Submitbutton to your form and emulate a click on the button. Doesn't it solve your purpose.

您可以Submit在表单中添加一个按钮并模拟单击该按钮。难道它不能解决你的目的。