我有一个这样的表格:
<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">
最佳答案
<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>
关于javascript - 将表单值附加到操作 url - 动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52420129/