如何同时重写和提交表单

How to rewrite and submit form at same time

本文关键字:提交 表单 重写 何同时      更新时间:2023-09-26

有一个表单我想同时提交和 url 重写。我可以通过在表单中添加onsubmit="rewrite_form(event);"选项来更改网址:

function rewrite_form(e) {    
  var form = document.forms[0];   // .getElementById("form1");
  window.location = '/search/' + form.f.value + '_' + form.t.value + '.htm/' + form.amt_from.value;            
  if (e && e.preventDefault) { e.preventDefault(); }
  return false;
}

网址更改,但表单的其他值未发布到 url 生成的页面。

只需更改窗体的操作属性即可。

function rewrite_form(e) {
    var form = documen.forms[0];
    form.action = 'newurl';
    //rest of code, make sure not to call e.preventDefault(); or return false
    //because the form will not get submitted
}

我得到了解决方案:

    function rewrite_form() {
//Create custom link here
----------------------------
----------------------------------
//create form submit action
var url = '/search/' +'your custom link';
document.getElementById('FormId').action = url;
document.FormId.submit();
}