如何同时重写和提交表格

时间:2013-11-04 04:05:16

标签: javascript forms post url-rewriting

我想提交一个表单并同时重写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;
}

网址更改但其他形式的值未发布到网址生成页面。

3 个答案:

答案 0 :(得分:0)

只需改变表单的动作属性即可。

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
}

答案 1 :(得分:0)

我得到了解决方案:

    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();
}

答案 2 :(得分:0)

我会这么认为:

  1. console.log("form:",form,"f:",f.value,"t:",t.value,"amt_from",amt_from.value) 检查我的表格价值
  2. 如果一切正确,请查看以下答案:Posting parameters to a url using the POST method without using a form