如何设置数据库中的值选择的选项?

时间:2013-07-13 04:51:50

标签: jquery selection selected cordova-2.0.0

我创建了注册页面和编辑页面,其中包含3个选择选项。在注册用户时,我将数据存储在数据库中,当用户单击编辑页面时,该数据库包含选择选项值,我可以使用从数据库中提取的数据填充文本字段,但是我无法选择选择选项我从数据库获得的值,我以前使用的代码是:

company变量存储来自数据库的选项值数据

<select name="select-choice-a" id="select-choice-a" data-native-menu="false" data-theme="a">
     <option>Select Company</option>
        <option value="standard">facebook</option>
        <option value="rush">Linked In</option>
        <option value="express">Skype</option>
        <option value="overnight">Twitter</option>
    </select>

的JavaScript

function fetchdetails(){
db.transaction(function(tx){
    tx.executeSql(select_details,[ids,nam1],function(tx,results){
        for(var i=0;i<results.rows.length;i++){
        first_name=results.rows.item(i).frist_name;
        last_name=results.rows.item(i).last_name;
        company=results.rows.item(i).company;
        title=results.rows.item(i).title;
        industry=results.rows.item(i).industry;
        owner=results.rows.item(i).owner;
        email=results.rows.item(i).email;
        landline=results.rows.item(i).landline;
        mobile_no=results.rows.item(i).mobile;
        fax_no=results.rows.item(i).fax_no;
        website=results.rows.item(i).website;
        street=results.rows.item(i).street_name;
        city=results.rows.item(i).city;
        region=results.rows.item(i).region;
        pincode=results.rows.item(i).pincode;
        country=results.rows.item(i).country;
        linkedin=results.rows.item(i).linkedin_link;
        twitter=results.rows.item(i).twitter_link;
        facebook=results.rows.item(i).facebook_link;
        skype=results.rows.item(i).skype_link;
        additionalinfo=results.rows.item(i).additional_info;

        }
    });
});
}

var index='';
var x=document.getElementById('select-choice-a');
alert(x.options.length);
for(var i=0;i<x.options.length;i++){
    if(x.options[i].value===company)
        {
        alert("I am in select option"); 
        index=i;
        alert("index of select-choice-a"+index);
        break;
        }
}
x.options[index].selected=true;

1 个答案:

答案 0 :(得分:0)

替换你的代码

var index='';
var x=document.getElementById('select-choice-a');
alert(x.options.length);
for(var i=0;i<x.options.length;i++){
    if(x.options[i].value===company)
        {
        alert("I am in select option"); 
        index=i;
        alert("index of select-choice-a"+Index);
        break;
        }
}
x.options[index].selected=true;

<script type="text/javascript">
var company='';
fetchdetails();
var x=document.getElementById('select-choice-a');
var index=0;
for(var i=0;i<x.options.length;i++){
    if(x.options[i].value===company)
        {
        alert("I am in select option"); 
        index=i;
        alert("index of select-choice-a"+index);
        break;
        }
}
x.options[index].selected=true;
</script>