2012-09-12 17 views
0

我正在使用下拉和在下拉的變化我打電話 一個javascript。在javascript中更改表 類的內容使用jquery.Now問題是,如果我的下拉菜單需要被 再次改變,它不是所有,因爲Iam改變了JavaScript中的內容。HTML下拉與JavaScript,Jquery

function filterbyaptno(){ 
     var idno = document.getElementById("aplist").value; 
     alert(idno); 
      $.ajax({ 
      url:address , 
      type: 'POST',      
      data:"idno="+idno, 
      success:function(result) { 
       var numRecords = parseInt(result.rows.length); 
       if(numRecords>0) 
       { 
        var html = ''; 
        for(var i = 0; i<numRecords;i++) 
        { 
        html +='<div><table class="support" width="700" cellpadding="10px;"> 
         <tr bgcolor="#E8E8E8"><td width="7";>'+result.rows[i].firstname+'</td>' 
        +'<td width="350">'+result.rows[i].advicetoowners+'</td>' 
        +'<td width="7">'+result.rows[i].customdata+'</td><tr></table></div>' 
        } 
         $('.detais').replaceWith(html);//here i change the content of div 
       }    
       $(function() { 
         $('.droplist').change(function(){ 
          filterbyaptno();//here i call javascript while dropdown change 
         }); 
       });  

       } 
      }); } 

任何人都可以建議我該怎麼做才能accordngly改變其內容爲下拉值

回答

0

硬盤沒有任何顯示的HTML(尤其是類的細節元素)的說在猜測,它似乎

html +='<div><table cla 

應該

html +='<div class='details'><table cla 

編輯:

我看到你最初開始與具有類詳細信息的表。第一次觸發函數時,它將被一個包含類支持表的div所取代。

所以,你的HTML將從

<table class='details'> 
... 
... 
... 
</table> 

<div> 
<table class='support'> 
... 
... 
... 
</table> 
</div> 

真的是你想要的嗎?

+0

沒錯,就是.details元素存在,首先檢查? @enzflep,你可以是對的。 –

+0

喜,這是表類細節<表寬度= 「700」 類= 「detais」 CELLPADDING = 「10px的;」> ​​​​ – Indira

+0

噢,以及在這種情況下,你有點進一步關閉比我想象的要多。 replaceWith替換整個元素。由於您從具有類「詳細信息」的表開始,因此如果希望在首次droplist.onchange觸發後獲得相同的行爲,則應該用另一個具有類詳細信息的表替換它。查看我的答案以獲得更新。 – enhzflep

0

從您的成功ajax調用中刪除此項。它不應該在那裏。

$(function() { 
    $('.droplist').change(function(){ 
     filterbyaptno();//here i call javascript while dropdown change 
    }); 
}); 

並把它的功能filterbyaptno外,使用.live來代替。

$('.droplist').live('change', function(){ 
    filterbyaptno();//here i call javascript while dropdown change 
}); 
0

嘗試更多的東西是這樣的:

function filterbyaptno() { 
    var idno = $("#aplist").val(); 
    $.ajax({ 
     url: address, 
     type: 'POST', 
     data: { 
      idno: idno 
     } 
    }).done(function(result) { 
     if (result.rows.length) { 
      var html = ''; 
      for (var i = 0; i < result.rows.length; i++) { 
       html += '<div><table class="support" width="700" cellpadding="10px;">'; 
       html += '<tr bgcolor="#E8E8E8"><td width="7";>'+result.rows[i].firstname+'</td>'; 
       html += '<td width="350">'+result.rows[i].advicetoowners+'</td>'; 
       html += '<td width="7">'+result.rows[i].customdata+'</td><tr></table></div>'; 
      } 
      $('.detais').replaceWith(html); 
     } 
    }); 
} 

$(function() {  
    $(document).on('change', '.droplist', filterbyaptno); 
});