2014-05-14 27 views
0

我在javascript中有一个查询字符串,如下所示:Default.aspx?7KCN0008-001当我尝试将7KCN0008-001插入到我获得的文本框中时[对象对象]我怎么能把这里的值放在这里的文本框是我的:如何在javascript中获取值查询字符串并将其放入文本框中

function test() { 
    var urlParams; 
    (window.onpopstate = function() { 
     var match, 
      pl = /\+/g, // Regex for replacing addition symbol with a space 
      search = /([^&=]+)=?([^&]*)/g, 
      decode = function (s) { return decodeURIComponent(s.replace(pl,"")); }, 
      query = window.location.search.substring(1); 

     urlParams = {}; 
     while (match = search.exec(query)) 
      urlParams[decode(match[1])] = decode(match[2]); 
    })(); 
$('#txtStockCode').val(urlParams[decode(match[1])]); 
} 

回答

1

我可能会吠叫错误的树,但正如你在你的问题中写的,如果URL类似Default.aspx?7KCN0008-001,你可以通过这段代码得到字符串7KCN0008-001

query = window.location.search.substring(1); 

所以我GUSS你可以简单地把这个字符串转换成文本

$('#txtStockCode').val(query); 

location.search该文件是在这里:
http://www.w3schools.com/jsref/prop_loc_search.asp

希望这有助于。


编辑:

好吧,如果查询7KCN0008-001 SAVSS0.85B 2180 916941-000%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20 917418-060,我想你可以做这样的事情:

HTML:

<input type="text" id="t1"> 
<input type="text" id="t2"> 
<input type="text" id="t3"> 
<input type="text" id="t4"> 
<input type="text" id="t5"> 
<input type="text" id="t6"> 

的Javascript:

var query = window.location.search.substring(1); // "7KCN0008-001 SAVSS0.85B ...." 
var decoded = decodeURI(query); 
var ary = decoded.replace(/\s{2,}/g, ' ').split(' '); 
for (var i = 0; i < ary.length; i++) { 
    $("#t"+(i+1)).val(ary[i]); 
} 

DEMO JSfidle is here:http://jsfiddle.net/naokiota/pH4mB/2/

希望这会有所帮助。

+0

还有一件事,如果我有这样的URL:Default.aspx?7KCN0008-001 SAVSS0.85B 2180 916941-000%20%20%20%20%20%20%20%20%20%20%20%20 %20%20%20%20%20%20%20%20%20 917418-060,我怎样才能把7KCN0008-001,SAVSS0.85B,2180,916941-000,917418-060放在自己的文本框中,比如6个文本框? – billah77

+0

@ billah77,好的。你的意思是有六个值由空格分隔? (看起来有五个值。) – naota

+0

我的坏有五个值 – billah77

0

我想你是混淆了数据结构。你首先将它定义为一个对象,然后你说它是一个数组。

urlParams = {}; 

urlParams[decode(match[1])] = decode(match[2]); 

你需要找出解码的值(匹配[1]),并在.VAL方法,其数组使用此....

$('#txtStockCode').val(urlParams[decode(match[1])]); 
+0

好吧,因为我真的很困惑我怎么能改变数据的结构 – billah77

+0

:-)没有数据的结构 - 数据结构。 Array vs Object –

相关问题