1

I have created registration page and Edit page which contains 3 select options. While registering a user I store the data in database which contains the select option value when user click on the edit page I am able to fill the text fields with the data that I fetch from data base but I am not able to select the select option which the value that I get from data base the code which I used to do this is:

the company variable store the data of option value from data base

<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;
4

1 回答 1

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>
于 2013-07-13T05:12:46.177 回答