简体   繁体   English

按下提交按钮后更改表单值

[英]Change form values after submit button pressed

[EDIT] After a lot of digging around, I found out that the problem was in how I integrated the CKEditor into my page. [编辑] 经过大量挖掘,我发现问题在于我如何将 CKEditor 集成到我的页面中。 The simple and obvious way does work in this case, as laid out in the accepted answer.正如接受的答案中所述,在这种情况下,简单而明显的方法确实有效。

Hi,你好,

I need to change the values of a form, after the submit button has been pressed, but before the actual submission has taken place.我需要在按下提交按钮之后,但在实际提交发生之前更改表单的值。

I've tried hooking into the "submit" event of the form, and changing the text field values there manually, but it looks like that doesn't actually change the values submitted.我已经尝试连接到表单的“提交”事件,并手动更改文本字段值,但看起来这实际上并没有更改提交的值。

Any ideas?有任何想法吗?

I'm curious about your statement that the submit handler didn't work for you. 我很好奇你的声明submit处理程序不适合你。 It does for me. 它对我有用。 I've used it for years to fill in hidden fields before sending forms in; 我已经用它多年来填写隐藏的字段,然后再发送表格; should work for other form fields as well. 应该适用于其他表单字段。

Example ( live copy ): 示例( 实时复制 ):

HTML: HTML:

<form id='theForm'
    action='http://www.google.com/search'
    method='GET' target='_new'>
      <label>Search for:
        <input type='text' name='q' id='txtSearch'></label>
      <input type='submit' id='btnSearch' value='Search'>

JavaScript: JavaScript的:

window.onload = function() {

  document.getElementById('theForm').onsubmit = function() {
    var txt = document.getElementById('txtSearch');
    txt.value = "updated " + txt.value;
  };
};​

Tested and working on IE6 and IE7 on Windows, and Chrome, Firefox, and Opera on Linux. 在Windows上测试并使用IE6和IE7,在Linux上测试Chrome,Firefox和Opera。


Update : Based on your comment below, you're using jQuery. 更新 :根据您的评论,您正在使用jQuery。 Works fine using jQuery for everything as well: 使用jQuery也可以正常工作:

$('#theForm').submit(function() {
  var txt = $('#txtSearch');
  txt.val("updated " + txt.val());
});

Live example Tested and working on the same set of browsers. 实例在同一组浏览器上测试和工作。 This version uses a more open search rather than an id , and also still works. 此版本使用更开放的搜索而不是id ,并且仍然有效。

You need to prevent the default submit action and then resubmit the form yourself manually: 您需要阻止默认的提交操作,然后手动重新提交表单:

$('form').submit(function(e) {
    e.preventDefault();

    // do your processing

    this.submit(); // call the submit function on the element rather than 
                   // the jQuery selection to avoid an infinite loop
});

Did you try adding function on click JavaScript event on the submit button and changing the values? 您是否尝试在提交按钮上单击JavaScript事件添加功能并更改值? It may work because client script will execute first 它可能有效,因为客户端脚本将首先执行

You could use formData event.您可以使用formData事件。

The formdata event fires after the entry list representing the form's data is constructed. formdata 事件在构建表示表单数据的条目列表后触发。 This happens when the form is submitted, but can also be triggered by the invocation of a FormData() constructor.这在提交表单时发生,但也可以通过调用 FormData() 构造函数来触发。

formElem.addEventListener('formdata', (e) => {
  const formData = e.formData;
  //no need change document.getElementById().value = "";
  formData.set('field1', 'foo');
  formData.set('field2', 'bar');
  //updated formData
});

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

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