2015-11-06 52 views
-1

所以使用jQuery我想取我的網址值例如:設置查詢字符串參數爲輸入值

/finance-proposal?value=BD07UWT 

並將其添加到這個輸入元素我的網頁上:

<input name="field[21]" id="field21" class="input-text required-entry " style="" value="" type="text"></input> 

我試圖解析url值,但我沒有成功。

任何想法什麼是頁面加載時用jQuery做這個最簡單的方法?

謝謝,尼克

+0

有許多可用於捕獲查詢參數插件分配值。然後你可以使用val()來設置輸入。 – isherwood

+1

看看這裏http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript –

+0

使用一個很好的URL解析庫。例如,這個:https://github.com/allmarkedup/purl你的當前位置存儲在window.location中。 – nagylzs

回答

0

使用getParameterByNameHow can I get query string values in JavaScript?),以獲得從querystring下面提供的價值。您可以使用.val()

var getParameterByName = function(name) { 
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); 
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), 
    results = regex.exec(location.search); 
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); 
}; 
var value = getParameterByName('finance-proposal'); 
$('.input-text required-entry').val(value); 
+0

當您複製其他問題的答案時,應該給出歸屬 – charlietfl

+0

@charlietfl,我同意但是'getParameterByName'在我的許多幫助程序中使用(儘管它來自其他某個帖子),但我沒有它的來源。如果這讓你失望,那我就可以了! – Rayon

+0

http://stackoverflow.com/a/901144/1175966 – charlietfl