【问题标题】:how to set select option selected by value from database?如何设置从数据库中按值选择的选择选项?
【发布时间】:2013-07-13 04:51:50
【问题描述】:

我创建了包含 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;

【问题讨论】:

  • 你想做什么?请确认
  • 我想要的是当用户选择编辑选项时,编辑页面将打开并且该页面将包含来自数据库的数据。我可以使用数据库中的数据设置字段我还希望选择选项设置为使用我从数据库中获得的值。

标签: jquery selection selected cordova-2.0.0


【解决方案1】:

替换你的代码

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>

【讨论】:

  • 我用这个方法来设置选择选项 var x=2; document.getElementById("select-choice-a").selectedIndex=x;它在浏览器上工作,但在模拟器上运行时它不起作用。我的意思是它没有选择设置选择选项。
  • @NeeravShah,公司变量包含什么值?是“标准”还是“脸书”??
  • 公司变量包含标准值。
  • 嘿,我试过它可以工作,但你能告诉我为什么当我尝试从数据库加载公司价值时它不工作。意味着我调用一个从数据库中获取数据的函数,然后它应该将选择的选择选项设置为该值。
  • @NeeravShah,编写将值加载到公司变量中的代码。
猜你喜欢
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
  • 2015-04-14
  • 2013-03-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
相关资源
最近更新 更多