簡體   English   中英

如何在網址中的文本框中添加用戶輸入以打開新網頁?

[英]How to add user input in a text box into the url to open a new webpage?

請幫我解決一下這個 。 我的問題是-我正在嘗試為我的博客構建一種語言識別工具。 我目前在JS方面很虛弱,所以遇到了困難。

我要做的就是放一個文本框,要求用戶輸入要檢測的語言。 然后,我必須將用戶輸入傳遞到url中,即q =“ input from text box”

https://www.googleapis.com/language/translate/v2/detect?key=123&q= “來自文本框的輸入”請提供幫助。

我應該怎么用window.open()做到這一點?

<input type="text" id="lang" />

window.open('https://www.googleapis.com/language/translate/v2/detect?key=123&q='+document.getElementById('lang').value);

我將使用jQuery,因為您將需要使用JSONP

<div id="results"> </div>

....

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: 'https://www.googleapis.com/language/translate/v2/detect?key=123&q=' + $('inputId').val() + '&callback=?',
    success: function (data) {
          $.each(response.data.detections, function() {
            $('#results').text(JSON.stringify($(this)[0].language));
        })
    }
}); 

我忽略window.open,因為響應是該請求的JSON。 您可以隨時顯示它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM