select控件獲取text值示例代碼

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
   <nav title="首頁">
   </nav>
   <div  width="100%" height="100%">
     <select id="select" value="" width="100" >
   </select>
   </div>
</body>
<script>
    var index = 0;
    //原始數據源
    var items = [{"id":"01","city":"上海"},{"id":"02","city":"北京"},
                 {"id":"03","city":"廣州"},{"id":"04","city":"深圳"},
                 {"id":"05","city":"杭州"},{"id":"06","city":"南京"}];
    var selcetItems = new Array();
    for(var i = 0; i < items.length; i ++){
        
        var item = {};
        item.text = items[i].city;
        item.value = items[i].id+"_"+items[i].city;
        selcetItems.push(item);
    }           
    select.setItems(selcetItems);
    
    select.addEvent("onChange",function(oldValue,newValue){
       if($M.isIOS){
          log(newValue)
          var arr = newValue.split("_");
          alert(arr[1])
       }
       else
       {
         if(index>0){
          log(newValue)
          var arr = newValue.split("_");
          alert(arr[1])
         }
         index ++; 
       }
     })
    
</script>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章