![](/img/trans.png)
[英]remove all the option from drop down list and then set a value in drop down list using javascript
[英]Pass a value from a URL into a Drop Down List using Javascript
我有一個網址如下
http://*****************?ContentId=1335&clientName=Sam&clientPhone=123&clientEmail=an@gmail.com&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的新手?
如果您已經填充了下拉列表,那么這應該可以從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;
}
}
確保通過綁定到文檔就緒事件來打印選擇的標記后運行該標記,或者在打印下拉列表的標記后將其包括在頁面中的某處。 它使用您包含的代碼中的第四個變量,因此在運行此變量時應已運行該變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.