2011-12-27 28 views
1

當用戶在表中添加一行時,它應該在每行的「圖像」列下顯示輸入「文件」功能,以便用戶可以爲每行選擇一個文件。但是當我添加一行時,它不顯示行內的文件輸入。如何在新行中添加輸入類型「文件」?

下面是html代碼:

<table id="qandatbl" border="1"> 
    <tr> 
     <th class="image">Image</th> 
    </tr> 
    </table> 

下面是jQuery代碼

function insertQuestion(form) { 

     var $tr = $("<tr></tr>"); 
     var $image = $("<td class='image'></td>"); 

     function image(){ 

     var $this = $(this); 
     var $imagefile = $("<input type='file' name='imageFile' id='imageFile' />").attr('name',$this.attr('name')) 
         .attr('value',$this.val()) 

      $image.append($imagefile); 

     }; 

     $tr.append($image); 
     $('#qandatbl').append($tr); 


    } 
+1

你喜歡PHP語法? JavaScript中不需要$ variable。 'variable'(沒有$)就足夠了。 – Armin 2011-12-27 15:16:14

+1

@Armin:當名稱代表一個jQuery對象時,這是一個被廣泛採用的約定,在變量名稱前面加上'$'。 – 2011-12-27 15:18:36

+0

哦!從來沒有聽說過這個慣例,但很好知道;-) – Armin 2011-12-27 15:22:54

回答

0

您目前的代碼片段看起來很混亂。試試這個:http://jsfiddle.net/66qAR/

<form id="idOfTheForm" action="?" method="post"> 
<table border="1"> 
    <tr> 
     <th class="image">Image</th> 
    </tr> 
</table> 
</form> 

而且此javascript:

function insertQuestion(form) { 
    var imageField = $('<input />') 
     .attr({ 
      type: 'file', 
      name: 'imageFile', 
      id: 'imageFile' 
     }); 
    $(form).find('table').append($('<tr />').append(imageField)); 
} 
insertQuestion($('#idOfTheForm')); 
+0

這是馬甲回答,謝謝 – 2011-12-27 15:53:07

2

我看不到的內部函數image()任何地方的電話。更多的爲什麼你有這樣的內在功能?解決這個問題很可能會解決您的問題。

1

這樣的事情,但我的問題是,什麼是this?由於您目前生成的功能在以後的任何操作中都未使用,因此我當前刪除了該功能,但無法確定$(this).val()會產生什麼結果。

你也通過form參數,但不使用它?

function insertQuestion(form) { 

    var $tr = $("<tr></tr>"); 
    var $image = $("<td class='image'></td>"); 


    var $imagefile = $("<input type='file' name='imageFile' id='imageFile' />").attr('name',$this.attr('name')) 
        .attr('value',$(this).val()) 
        .appendTo($image); 


    $tr.append($image); 
    $('#qandatbl').append($tr); 

} 
相關問題