2011-12-21 129 views
0

我有一个网址如下传递从URL的值划分为下拉列表使用Javascript

http://*****************?ContentId=1335&clientName=Sam&clientPhone=123&[email protected]&clientCountry=United%20Kingdom. 

clientCountry一个下拉我的形式列表的名字,我要传递的价值(王国)到使用JavaScript的下拉列表中。

我已经能够做到这一点至今

function getUrlVars() 
{ 
var vars = [], hash; 
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
for(var i = 0; i < hashes.length; i++) 
{ 
hash = hashes[i].split('='); 
vars.push(hash[0]); 
vars[hash[0]] = hash[1]; 
} 
return vars; 
} 
var first = getUrlVars()["clientName"]; 
var second = getUrlVars()["clientPhone"]; 
var third = getUrlVars()["clientEmail"]; 
var fourth = getUrlVars()["clientCountry"]; 

现在我不知道该怎么第四传递变量到下拉列表中。任何人都可以协助,因为我是一个JavaScript的总新手?

+0

是否要根据URL参数预先选择现有下拉列表中的值? – 2011-12-21 11:22:00

+0

是的,这是正确的,携带数据的网址需要填写表单。 – Nirm 2011-12-22 16:18:20

回答

0

如果您已经填充的下拉列表中,这应该从URL预选值工作:

var dropDowns = document.getElementsByTagName('select'), opts, i; 
for(i = 0, l=dropDowns.length;i<l;++i){ 
    if(dropDowns[i].name === 'countryList'){ 
    opts = dropDowns[i].getElementsByTagName('option'); 
    for(i = 0, l = opts.length;i<l;++i){ 
     if(opts[i].value === fourth){ 
     opts[i].selected = true; 
     break; 
     } 
    } 
    break; 
    } 
} 

确保此运行您通过绑定到文档准备事件打印在选择标记之后,或者在打印下拉列表的标记后将其包括在页面的某个位置。它使用您包含的代码中的第四个变量,以便在运行此应用程序时运行。

+0

非常感谢您 – Nirm 2012-11-27 18:24:32

+0

不客气:)如果答案对您有帮助,您应该注意或接受答案。 – 2012-11-28 05:19:52

相关问题