Build URL from two form fields [closed]

2019-09-21 19:08发布

问题:

Essentially, we have two form fields asking for users to supply us with two alphanumeric strings, and a URL needs to be constructed from the two. However, there is a basic structure to the URL that needs to be kept in place.

Users will input the requested numbers in two text fields on our site, then click a submit button. At that point the input data needs to be inserted into the URL and opened in their browser.

Example: URL.com/FixedData + UserEntry1 + FixedData + UserEntry2 –– UserEntry1 & UserEntry2 will be inserted between the fixed data.

The finished URL would appear as: http://URL.com/FixedDataUserEntry1FixedDataUserEntry2

回答1:

You pretty much answered your question.

HTML

<input id='UserEntry1' type='text'>
<input id='UserEntry2' type='text'>

Javascript

var URLBase = "http://URL.com/fixeddata1";
var TrailingFixedData = "fixeddata2";

finalURL = URLBase + document.getElementById('UserEntry1').value + TrailingFixedData + document.getElementById('UserEntry2').value;

Or if you're using jQuery:

finalURL = URLBase + $('#UserEntry1').val() + TrailingFixedData + $('#UserEntry2').val();


回答2:

http://jsfiddle.net/4M3q3/

HTML:

<input id="one"><input id="two">
<button id="open">Open</button>

JS:

$('#open').click(function() {
    var fixedData1 = 'http://www.google.com/#q=',
        fixedData2 = '+',
        userEntry1 = $('#one').val(),
        userEntry2 = $('#two').val();

    var newWindow = window.open(fixedData1 + userEntry1 + fixedData2 + two, '_blank');
    newWindow.focus();
});