2014-11-06 79 views
3

我有一个php动态添加行的形式(点击按钮后行添加)。我想填充字段值“xx”,我想在jQuery中做到这一点。如何使用jquery动态添加行形式的值?

此循环在jquery中创建动态添加的行。我想,以填补值 “XX” 添加字段:

while($personsArrayLength>2){ 
     echo ' 
      <script> 
      $(document).ready(function(){ 
       var i = 2; 
       var rowTemplate2 = jQuery.format($("#template2").html()); 

       rowTemplate2.value = "xx"; 
       addRow2(); 

       function addRow2(){ 
        var ii = i++; 
        $("#app_here2").append(rowTemplate2(ii)); 
        $("#delete_" + ii).click(function(){ 
         $("#row_" + ii).remove(); 
        }); 
       } 
      }); 
     </script> 
    '; 

以下是一个HTML:

function addRows2(){ 

    global $personsError_css; 
    $personsArray = $_POST['persons']; 
    JB_var_dump($_POST['whichRow']); 
     $html = '<table id="template2" align="right" style="display:none; "> 
      <tr id="row_{0}"> 
       <td><input type="text" size="52" id="persons" name="persons[]" maxlength="100"></td> 
       <td><img src="/../_img/row_del.png" id="delete_{0}" alt="usun"></td> 
      </tr> 
     </table> 
     <table id="list2" style="margin-left:200px;"> 
      <thead > 
       <tr> 
        <th></th> 
        <th></th> 
       </tr> 
      </thead> 

      <tbody> 
       <tr> 
        <td><input type="text" size="52" name="persons[]" maxlength="100" style="'.$personsError_css.';" value="'.$personsArray[1].'"></td> 
        <td></td> 
       </tr> 

       <tr> 
        <td colspan="2" id="app_here2"></td> 
       </tr> 
      </tbody> 
     </table>'; 
     return $html; 
} 

这是正确填写表格
enter image description here

在这种epty领域我想以增加值“xx” enter image description here

对不起,我的英语。

如何设置添加行的值?我的代码中应该改变什么? 感谢您的帮助。

+0

你想设置一个占位符?直到人类输入东西时,占位符将填充文本框,否则......'rowTemplate2.find('input:empty').val('xx')' – Mephiztopheles 2014-11-06 12:08:36

回答

1

更改 'addRow2' 这样的:

function addRow2(){ 
    var ii = i++; 
    $("#app_here2").append(rowTemplate2(ii)); 
    //UPDATE: var rowTemplate2 is not a jQuery Object, as i thought 
    $("#template2").find("input:empty").val("xx");; // added this row 
    $("#delete_" + ii).click(function(){ 
     $("#row_" + ii).remove(); 
    }); 
} 
+0

它不起作用。脚本在瞬间停止: 'rowTemplate2.find('input:empty')。val('xx')'并且不继续。 – 2014-11-06 12:15:39

+0

在什么时候? – Mephiztopheles 2014-11-06 12:16:18

+0

add';' ..所以再次复制我的解决方案,因为我错过了一个分号..但我不认为,这会导致问题...你可能按F12并说我,你看到了什么? – Mephiztopheles 2014-11-06 12:19:10

相关问题