如何在动作标签中传递参数?

时间:2014-10-17 15:44:55

标签: javascript jquery html forms

我想将res变量作为表单操作传递。这可能与HTML有关吗?

 <script>
    var name =$("#name").val();
    var file =$("#file").val();
    var res= localhost:8080 + "/test/reg?&name="+name+"&file=" +file  ;
    </script>
    <form action="res" id ="form" method="post" enctype="multipart/form-data">
    Name:<input type="text" name="name" id="name"/>
    Upload:<input type="file" name="file" id="file"/> 
    </form>

document.forms["form"].submit();

1 个答案:

答案 0 :(得分:1)

在提交之前设置action属性:

var form = document.forms["form"];
form.action = res;
form.submit();

如果用户可以手动提交此表单,则可以使用onsubmit事件:

form.onsubmit = function(){
    this.action = res;
};