2010-09-18 92 views
1

您好我仍然在學習Jquery和訪問表單元素有問題。我的腳本打開一個div,然後用預先寫好的html表格填充它。我使用ajax來獲取表單和jQuery。這一切工作正常,我可以看到我的表格在其中的div框。問題是我想填充innerhtml表單,我已經嘗試過javascript和jquery,但是我的代碼都不會填充文本框。訪問與JQuery的innerhtml

divbox = "fixedbox" 
// open up the div 
setdiv(divbox,100,600,800) 

    // JQ - ajax 
    $(document).ready(function(){ 
    $("#" + divbox).load("../customer_rm/display_email_send.php"); 
    }); 

    // neither of these will produce any text 
    document.getElementById('mailcc').value = "test"; 
    $("#mailcc").val("test"); 

如果需要,這裏是表格。任何人都可以提供任何建議,我正在做什麼?

   <?php 
      echo " 

      <style type='text/css'> 
      table {border: 1px solid black} 
      td, tr {border: 0} 

      .bdr { 
       border: 4px solid black ; 
      } 
      .white { 
       background-color:#FFF ; 
      } 

      </style> 
      <div align='center'> 

      <br><br /> 

      <table width='700' border='2' bgcolor='#CCCCCC'> 
      <form id = 'mail' name='mail' > 
       <tr > 
       <td width='20'>&nbsp;</td> 
       <td width='50'>&nbsp;</td> 
       <td width='50'>&nbsp;</td> 
       <td >&nbsp;</td> 
       <td width='20' >&nbsp;</td>  

       </tr> 

       <tr> 
       <td>&nbsp;</td> 
       <td rowspan='3'> 
       <input type='button'id='send' value='Send' 
       style='width:60px; height:40px ' 
       /><hr> 
       <input type='button' value='Close' 
       style='width:60px; height:20px '      onclick='fadeout()' 
       /> 
       </td> 
       <td><input type='button' value='To :' /></td> 
       <td><input type='text' class='white' id='mailto' size='80' /></td> 
       <td>&nbsp;</td> 
       </tr> 

       <tr> 
       <td>&nbsp;</td> 

       <td><input type='button' value='Cc :' /></td> 
       <td><input type='text' class='white' id='mailcc' size='80' /></td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td>&nbsp;</td> 

       <td><input type='button' value='Bcc :' /></td> 
       <td><input type='text' class='white' id='mailbcc' size='80' /></td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td>&nbsp;</td> 
       <td colspan='3'> 
        <textarea name='mail_body' id='mail_body' class='white' 
        style='height:380px; width:600px; bgcolor:#fff ' > 
        </textarea> 
       </td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       </tr> 
      </form> 
      </table> 


      </div> 
      " ; 

這是div框

   #fixedbox { 
       position:absolute ; 
       width: 20px; 
       height: 20px; 
       top: 40px ; 
       border: 2px solid black; 
       background-color:#CCC; 
       left:50% ; 
       margin-left:-300px; 
       visibility:hidden ; 
       overflow:hidden ; 
      } 

回答

2

mailcc是輸入字段。您不應使用innerHTML/html()來訪問輸入的值。相反:

document.getElementById('mailcc').value = "test"; 
$("#mailcc").val("test"); 

這兩個都是非常正確的。但是,假設mailcc包含在display_email_send.php中,那麼此時它尚未加載。您將只能夠訪問它之後的內容已經load()版,您可以通過做一個合格的回調函數:

$(document).ready(function(){ 
    $('#'+divbox).load('../customer_rm/display_email_send.php', function() { 
     $('#mailcc').val('test'); 
    }); 
}); 
+0

感謝您抽出寶貴時間看看這個給我,歡呼 – Mick 2010-09-18 10:51:16

0
document.getElementById('mailcc').innerHTML = "test"; 
+0

感謝您嘗試,但沒有結果!米克 – Mick 2010-09-18 10:28:23