Build URL from form fields with JavaScript or jQuery

后端 未结 3 993
时光取名叫无心
时光取名叫无心 2021-02-07 06:22

I\'m trying to create a URL builder form with JavaScript or jQuery.

Basically, it will take the value of the two form fields, add them to a preset URL and show it on a th

3条回答
  •  野的像风
    2021-02-07 06:47

    Use something like...

    var inputs = $('#form1').find('input[type=text]').not('#url');
    var str = "http://www.base.url/path/file.ext?"
    inputs.each(function (i, item) {
        str += encodeURIComponent(item.name) + "=" + encodeURIComponent(item.value) + "&";
    });
    $('#url').val(str);
    

    This will select all s on in form1 with type='text', and concatenate them into a query string. See encodeURIComponent().


    Orrrr.....you could just use .serialize(). Thank you, prodigitalson.

提交回复
热议问题