2009-12-07 38 views
1

我在我的應用程序中使用Javascript。在我的表,我有一個名爲industry列包含值一樣將下拉框設置爲在Javascript中選中

id 69 
name :aruna  
username :aruna 
email :[email protected] 
password: bd09935e199eab3d48b306d181b4e3d1:i0ODTgVXbWyP1iFOV 
type : 3  
industry: | Insurance | Analytics | EIS and Process Engineering 

其實這個行業的價值是從下拉框中選擇多..插入

我現在想像負荷,使我的表格爲包含這些值

其中行業是下拉框

<select id="ind1" moslabel="Industry" onClick="industryfn();"mosreq="0" multiple="multiple" size="3" class="inputbox" name="industry1[]">'+ 

    <option value="Banking and Financial Services">Banking and Financial Services</option> 

    <option value="Insurance">Insurance</option> 

    <option value="Telecom">Telecom</option> 

    <option value="Government ">Government </option> 

    <option value="Healthcare &amp; Life sciences">Healthcare & Life sciences</option> 

    <option value="Energy">Energy</option> 

    <option value="Retail &amp;Consumer products">Retail &Consumer products</option> 

    <option value="Energy, resources &amp; utilities">Energy, resources & utilities</option> 

    <option value="Travel and Hospitality">Travel and Hospitality</option> 

    <option value="Manufacturing">Manufacturing</option> 

    <option value="High Tech">High Tech</option> 

    <option value="Media and Information Services">Media and Information Services</option> 
</select> 

如何保持行業的值(|保險|肛門ytics | EIS和過程工程)如何選擇?

編輯: Window.onDomReady(函數(){ 用戶>獲取( '業'); $ S =爆炸( '|',$ STR) >

    var selectedFields = new Array(); 
        <?php for($i=1;$i<count($s);$i++){?> 

         selectedFields.push("<?php echo $s[$i];?>"); 
         <?php }?> 
         for(i=1;i<selectedFields.length;i++) 
        { 

           var select=selectedFields[i]; 
      for (var ii = 0; ii < document.getElementById('ind1').length; ii++) { 
        var value=document.getElementById('ind1').options[ii].value; 
      alert(value); 
      alert(select); 
                if(value==select) 
                { 

        document.getElementById('ind1').options[ii].selected=selected; 
                }//If 
           } //inner For 
        }//outer For 
     </script> 

? 爲什麼如此..請幫助我......

回答

0

將選定的值分解到一個數組中,使一個foreach循環爲貫穿所有選擇您選擇的離子,如果這些值匹配,只需在那裏選擇=「選擇」。

例子:

使用Javascript/PHP:

<script type="text/javascript"> 
      Array.prototype.in_array = function(p_val) { 
       for(var i = 0, l = this.length; i < l; i++) { 
        if(this[i] == p_val) { 
         return true; 
        } 
       } 
       return false; 
      } 

      // Swap this for normal onDomReady if you don't use jQuery 
      $(document).ready(function() { 
<?php 
$selected_from_db = "Insurance|Analytics|EIS and Process Engineering|Telecom"; 
$selected = explode("|", $selected_from_db); 
?> 

     var selected_fields = new Array(); 

<?php 
foreach ($selected as $key => $value) { 
    ?> 
      selected_fields.push("<?php echo $value;?>"); 
<?php 
} 
?> 

     var ind_dropdown = document.getElementById("ind1"); 

     for (var i = 0; i < ind_dropdown.length; i++) 
     { 
      if (selected_fields.in_array(ind_dropdown.options[ i ].text)) { 
       ind_dropdown.options[ i ].setAttribute("selected", "selected"); 
      } 

      /*var select=selectedFields[i]; 
      for (var ii = 0; ii < document.getElementById('ind1').length; ii++) { 
        var value=document.getElementById('ind1').options[ii].value; 
      alert(value); 
      alert(select); 
                if(value==select) 
                { 

        document.getElementById('ind1').options[ii].selected=selected; 
                } 
           }*/ 
     } 

    }); 
     </script> 

HTML:

<body> 
     <select id="ind1" name="industry[]" multiple="multiple" size="5"> 
      <option value="Banking and Financial Services">Banking and Financial Services</option> 
      <option value="Insurance">Insurance</option> 
      <option value="Telecom">Telecom</option> 
      <option value="Government ">Government </option> 
      <option value="Healthcare &amp; Life sciences">Healthcare &amp; Life sciences</option> 
      <option value="Energy">Energy</option> 
      <option value="Retail &amp;Consumer products">Retail &amp; Consumer products</option> 
      <option value="Energy, resources &amp; utilities">Energy, resources &amp; utilities</option> 
      <option value="Travel and Hospitality">Travel and Hospitality</option> 
      <option value="Manufacturing">Manufacturing</option> 
      <option value="High Tech">High Tech</option> 
      <option value="Media and Information Services">Media and Information Services</option> 
     </select> 

    </body> 

我不建議混合PHP和JavaScript代碼壽。這太麻煩了,很難維護。我建議你在服務器端(PHP)或客戶端(AJAX)解析這些數據。

+0

請看我的編輯..我已經完成了你所說的..但如果功能不工作。你呢? – useranon 2009-12-07 09:43:21

+0

用一個例子編輯我的答案。 – 2009-12-09 11:38:08

相關問題