【问题标题】:How to set value in dynamically added rows form using jquery?如何使用 jquery 在动态添加的行表单中设置值?
【发布时间】:2015-01-02 22:20:42
【问题描述】:

我在 php 中有一个动态添加行的表单(单击按钮后添加了行)。我想用值“xx”填充该字段,我想在 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;
} 

这是正确填写的表格

在这个空字段中,我想添加值“xx”

对不起我的英语。

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

【问题讨论】:

  • 您要设置占位符吗?直到人们输入占位符将填充其他文本框的内容.. rowTemplate2.find('input:empty').val('xx')

标签: javascript php jquery forms


【解决方案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();
    });
}

【讨论】:

  • 不幸的是,它不起作用。脚本立即停止:'rowTemplate2.find('input:empty').val('xx')' 并且不会继续。
  • 添加';' ..所以再次复制我的解决方案,因为我错过了一个分号..但我不认为这会导致问题...你可以按 F12 并告诉我,你看到了什么?
  • 发现我的错误。 var rowTemplate2 不是我想的 jQuery 对象
  • 不是分号。这是我点击 F12 后的链接:link
  • 好的...我期待控制台 :D ..您了解开发人员工具吗?您使用哪个浏览器?您的帖子中的代码有效吗?我不知道jQuery.format()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
相关资源
最近更新 更多