简体   繁体   English

在表单输入后附加URL

[英]Append URL with form input

This is my first attempt to write anything in javascript, although this does exactly as intended, I am sure it can be done simpler. 这是我第一次尝试用javascript编写任何东西,尽管这样做确实符合预期,但我相信它可以更简单地完成。 Not that this script is all that useful, just an exercise in learning something new. 并不是说这个脚本很有用,只是一个学习新知识的练习。 I was also trying not to use the evil document write. 我也试图不使用邪恶的文件写。

So what is the more elegant way of doing this? 那么,这样做的更优雅的方法是什么?

<html>
<body>

<input name="abc" type="text" id="foo">
<button onclick="AddInputValue()">Submit</button>

<p id="displayURL"></p>

<script>
function AddInputValue(){
var domain = "http://site.com?abc="
var qstring = document.getElementById("foo").value;
document.getElementById("displayURL").innerHTML=domain + qstring;
}
</script>
</body>
</html>

If you use jQuery : 如果您使用jQuery

<html>
    <!-- Include jQuery! -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <body>
        <form id="form1">
            <input name="abc" type="text" id="foo">
            <button type="submit">Submit</button>
        </form>

        <p id="displayURL"></p>

        <script>
            $(document).ready(function () {
                var form = document.getElementById("form1");
                $(form).submit(function () {
                    var domain = "http://site.com/?";
                    var data = $(this).serialize();

                    document.getElementById("displayURL").innerHTML = domain + data;

                    return false;
                });
            });
        </script>
    </body>
</html>

You can even add more form elements and the name of the element will match the query string. 您甚至可以添加更多表单元素,并且该元素的name将与查询字符串匹配。 http://jsfiddle.net/3muu6/ http://jsfiddle.net/3muu6/

Just posting the example in http://jsfiddle.net/3muu6/ . 只需将示例发布在http://jsfiddle.net/3muu6/中

Increased the number of inputs. 增加了输入数量。 This is basically what Google Analytics URL Builder does, and was the inspiration for this exercise. 这基本上就是Google Analytics(分析)URL Builder的工作,并且是本练习的灵感。

<html>
<head>
<!-- Include jQuery! -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</head>

<body>
 <form id="form1">
    <input name="abc" type="text" id="foo" /><br />
    <input name="def" type="text" id="bar" /><br />
    <input name="ghi" type="text" id="tar" /><br />
    <input name="jkl" type="text" id="boo" /><br />
<button type="submit">Submit</button>
</form>
<p id="displayURL"></p>


<script>
$(document).ready(function () {
    var form = document.getElementById("form1");
    $(form).submit(function () {
        var domain = "http://example.com/?";
        var data = $(this).serialize();

        document.getElementById("displayURL").innerHTML = domain + data;

        return false;
    });
}); 
</script>
</body></html>

Now how to omit a query-string pair when the user leaves an input value blank? 现在,当用户将输入值留空时,如何省略查询字符串对? Hmm. 嗯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM