将表单值附加到操作网址-动态

时间:2018-09-20 07:48:09

标签: javascript html forms url-routing

我有这样的表格:

<form action="http://localhost:8080/abc/def/gh" method="post">

我需要用当前主机名动态替换 localhost:8080

使用-window.location.hostname

例如,如果触发代码的当前主机名的地址为 192.222.1.333:8080 ,则代码应动态转换为:

<form action="http://192.222.1.333:8080/abc/def/gh" method="post">

1 个答案:

答案 0 :(得分:2)

<html>
<body>
    <form action="http://localhost:8080/abc/def/gh" id="action-form" method="post"></form>

    <script>
        var form = document.getElementById("action-form");
        form.action = form.action.replace("localhost:8080", window.location.hostname);
    </script>
</body>
</html>