如何将表单操作更改为文本输入?

时间:2015-08-27 20:28:30

标签: javascript forms action

我想根据输入值更改表单目标。 我怎么能这样做?

<form id="form" method="POST" action="x.php" onSubmit="">
  <input type="text" id="address" value="google.com">
  <input type="submit" id="go" value="Go">
</form>

示例:发送此表单时,请打开google.com。

2 个答案:

答案 0 :(得分:1)

我用 JavaScript: how to get value of text input field?  &安培; How to set form action through JavaScript?

所以我的结果:

<script type="text/javascript">
    function get_action(form) {
        form.action = 'http://'+document.getElementById("address").value;
    }
</script>

<form onsubmit="get_action(this);">
  <input type="text" id="address" value="google.com">
  <input type="submit" id="go" value="Go">
</form>

谢谢@Mike&amp; @PrashantValanda:*

答案 1 :(得分:0)

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> 

 <script type="text/javascript">
            $(document).ready(function () {
                $("#address").blur(function () {
                    var form = document.getElementById('form');
                        form.action = $("#address").val();
                });
            });
</script>

<form id="form" method="POST" action="x.php" onSubmit="">
  <input type="text" id="address" value="google.com">
  <input type="submit" id="go" value="Go">
</form>