简体   繁体   English

用户尝试提交指定表单时如何更改表单参数

[英]How to change form parameters when user trying to submit the specified form

Is there any way to change form parameters when user trying to submit the specified form? 用户尝试提交指定的表单时,是否可以更改表单参数?

Suppose that I have the following form: 假设我具有以下形式:

<form id="myform" method="port" action="/form.php">
  <input type="file" name="image1">
  <input type="submit">
</form>

So when the user clicks a submit button it makes a POST request to the /form.php that contains image1 parameter. 因此,当用户单击提交按钮时,它将对包含image1参数的/form.php发出POST请求。 I want to change this parameter to completely different base64-encoded image programmatically. 我想以编程方式将此参数更改为完全不同的base64编码图像。

Is it possible? 可能吗?

You can edit the values in jQuery's $('form').submit() event, as described in this answer: https://stackoverflow.com/a/1547963/5742681 您可以在jQuery的$('form').submit()事件中编辑值,如此答案中所述: https : //stackoverflow.com/a/1547963/5742681

You can replace the form values with anything you want, by creating a new data object and POST ing that instead of the original form values. 您可以通过创建新的数据对象并POST而不是原始表单值,将其替换为所需的表单值。

If you don't want to use jQuery's . 如果您不想使用jQuery的。 post() for some reason, you can also change the input values in $('form').submit() and return true, the standard submit event will happen after jQuery's. post()由于某种原因,您也可以更改$('form').submit()的输入值并返回true,标准的commit事件将在jQuery之后发生。

Add ID to your file input and a hidden input field so html looks like: 在您的文件输入和隐藏的输入字段中添加ID,因此html如下所示:

<form id="myform" method="post" action="/form.php">
  <input id="changeme" type="file" name="image1">
  <input id="hidden" type="hidden" name="base64img" value="">
  <input type="submit">
</form>

In jQuery you would take some actions on form submit like: 在jQuery中,您将对表单提交执行以下操作:

var frm = $('#myform');
frm.submit(function (ev) {
   frm.find('input#hidden').attr('value', 'data:image/png;base64,alotoftext');
   frm.find('input#changeme').remove();
});

EDIT: 编辑:

If you dont have access to server, add elemt with jQuery like: 如果您无权访问服务器,请使用jQuery添加elemt,例如:

$('<input>').attr({
    type: 'hidden',
    id: 'hidden',
    name: 'base64img',
    value: ''
}).appendTo('#myform');

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

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