简体   繁体   English

使用Ajax提交表单

[英]Submit a form with Ajax

I'm making a user script for a site, and my goal is to submit a form on a page that I haven't opened. 我正在为网站制作用户脚本,我的目标是在尚未打开的页面上提交表单。 If I remove all unneeded bits from the page with the form that I want to submit, this is what is left (censoring the links): 如果我使用要提交的表单从页面中删除所有不需要的位,则剩下的内容(检查链接):

<form action="http://foo.com/supply/" method="POST" name="supplyContractForm">
<input type="hidden" name="supplyContractData[selected][]" value="2244068">
<input type="type" name="supplyContractData[party_quantity][2244068]" value="123">  
<input type="text" value="0" name="supplyContractData[quality_constraint_min][2244068]">
<input type="submit" name="applyChanges">
</form>

It's all about the third line: with the 'value="123"'. 这一切都与第三行有关:带有'value =“ 123”'。 I want to change that value to "222". 我想将该值更改为“ 222”。 What do I do: I change the input value from "123" to "222", I press the submit button, and the form submits: the page reloads, and the value shown is "222". 我该怎么做:我将输入值从“ 123”更改为“ 222”,按下提交按钮,然后表单提交:页面重新加载,显示的值为“ 222”。 Exactly as I want. 正是我想要的。

Now, this was all manual, and I want it scripted. 现在,所有这些都是手动的,我希望将其编写为脚本。

This works: 这有效:

$("input:submit").click();

However, this doesn't work: 但是,这不起作用:

$("form").submit();

And this doesn't work either: 而且这也不起作用:

$.post($("form").attr("action"), $("form").serialize())

How can I post this form using Ajax, in a way I can change the value from http://foo.com/main/ ? 如何使用Ajax来发布此表单,从而可以从http://foo.com/main/更改值?

Note: I can only do things client-side, I'm just making a user script, and I can't see the server-side code. 注意:我只能在客户端执行操作,我只是在编写用户脚本,而看不到服务器端代码。

This would do the trick 这可以解决问题

Html, notice the addedd class for cleaner js: HTML,请注意清洁器js的添加类:

<form action="http://foo.com/supply/" method="POST" name="supplyContractForm">
    <input type="hidden" name="supplyContractData[selected][]" value="2244068">
    <input class="changer" type="type" name="supplyContractData[party_quantity][2244068]" value="123">  
    <input type="text" value="0" name="supplyContractData[quality_constraint_min][2244068]">
    <input type="submit" name="applyChanges">
</form>

Js: Js:

$('form input.changer').val('222');
$('input:submit').click();

The reason why $('form').submit() might not work would be because $('form') matches multiple element, ie do you have multiple forms in your html? $('form')。submit()可能不起作用的原因是因为$('form')匹配多个元素,即您的html中是否有多个表单? if so make the selector more unique, via either adding ids or classes so your selected becomes: 如果是这样,则通过添加ID或类使选择器更加独特,从而使所选内容变为:

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

Ajax 阿贾克斯

This doesn't use ajax though, for that you would need: 但是,这不使用ajax,因此您需要:

var form = $('form');
$.ajax({
    url: form.attr('action'),
    type: form.attr('method'),
    data: form.serialize(),
    success: function(){
        alert('Form posted via ajax!');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM