我有这样的表格:
<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">
答案 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>