Q
從URL中提取詞
5
A
回答
2
如果它僅僅是一個key=value
url中你可以這樣使用簡單的正則表達式:
var theValueYouWant = window.location.href.match(/keyword=(.+)/)[1]
然後設置這樣的輸入的值
$('input').val(theValueYouWant)
如果你想更徹底解析GET字符串,這個功能應該做...
gets = {};
$.each(location.search.replace(/^\?/,'').split('&'),function(k,v){
bits = v.split('=');
gets[bits[0]] = bits[1];
});
3
使用此: http://ajaxcssblog.com/jquery/url-read-get-variables/
走好運!
哦,那麼你可以使用下面的輸入字段中顯示它的值:
$("#inputId").val($.url.param("keyword"));
0
的jQuery少的解決方案:
<script type="text/javascript">
var $_GET=[],pairs=location.href.toString().substring(location.href.toString().indexOf("?")+1).split("&");for(key in pairs){pos=pairs[key].indexOf("=");$_GET[pairs[key].substring(0,pos)]=decodeURIComponent(pairs[key].substring(pos+1).replace(/\+/g," "))};
// Now just access with $_GET
// example...
keyword = $_GET["keyword"];
</script>
1
正則表達式的解決方案:
var S = window.location.search; // "?keyword=..." etc.
var T = S.match(/^\?(?:[^\b]*&+)?keyword=([^&]*)/);
if (T)
T = T[1]
else
T = "no keywords found"
如果多個值均爲「關鍵字「(前?keyword=XXX&keyword=YYY
),正則表達式只會找到這些值中的第一個(e.x. XXX
)。即使查詢字符串中有其他變量,該正則表達式也可以工作。
相關問題
- 1. 如何從URL中提取單詞?
- 2. 從url中提取域名
- 3. 從URL中提取部分
- 4. 從URL中提取查詢
- 5. 從URL中提取文本
- 6. 僅提取從URL
- 7. 從單詞圖像中提取字符
- 8. 從文件中提取單詞
- 9. 從文件中提取單詞
- 10. 從字符串中提取單詞?
- 11. 從文件中提取詞的部分
- 12. 如何從行中提取單詞
- 13. 從NLTK WordNet中單獨提取名詞
- 14. 從音頻剪輯中提取單詞
- 15. 從域名中提取單詞
- 16. 從數組中提取單詞
- 17. jQuery從字符串中提取單詞
- 18. 從Lucene索引中提取搭配詞
- 19. 從文本中提取名詞(Java)
- 20. 用golang從PDF中提取單詞?
- 21. 從序言列表中提取單詞
- 22. 從網站中提取單詞
- 23. 從多個詞典中提取到csv
- 24. 從句子中提取動詞
- 25. 從序言結構中提取單詞
- 26. Ruby:從字符串中提取單詞
- 27. 從Wordnet中提取單詞列表
- 28. 從文本中提取特定單詞
- 29. 從python中的URL中提取部分
- 30. 從ruby中的URL中提取錨點
漂亮和微小的插件+1 – Pinkie 2011-05-17 21:49:01
是的,我喜歡它。我把它加載到我的大部分網站當中是一種習慣,因爲它已經(快速,簡單地)訪問JS中的GET變量是多麼有用。 – Todd 2011-05-17 21:55:53