【问题标题】:Using JQuery to add or remove form elements使用 JQuery 添加或删除表单元素
【发布时间】:2009-10-14 06:27:37
【问题描述】:

我一直在尝试使用 JQuery 来添加或减去表单输入字段的函数。我一直在使用我找到的示例作为起点。

这是我目前所拥有的:

<script type="text/javascript">
        $(function(){
            // start a counter for new row IDs
            // by setting it to the number
            // of existing rows
            var newRowNum = 2;

            // bind a click event to the "Add" link
            $('#addnew').click(function(){
                // increment the counter
                newRowNum += 1;

                // get the entire "Add" row --
                // "this" refers to the clicked element
                // and "parent" moves the selection up
                // to the parent node in the DOM
                var addRow = $(this).parent().parent();

                // copy the entire row from the DOM
                // with "clone"
                var newRow = addRow.clone();

                // set the values of the inputs
                // in the "Add" row to empty strings
                $('input', addRow).val('');
                $('name', addRow).val('os' + newRowNum);

                // replace the HTML for the "Add" link 
                // with the new row number
                $('td:first-child', newRow).html('<input type="hidden" name="on' + newRowNum + 'value="Email Address ' + newRowNum + '>Recipient');

                // insert a remove link in the last cell
                $('td:last-child', newRow).html('<a href="" class="remove">Remove<\/a>');

                // loop through the inputs in the new row
                // and update the ID and name attributes
                $('input', newRow).each(function(i){
                    var newID = 'os' + newRowNum;
                    $(this).attr('id',newID).attr('name',newID);
                });

                // insert the new row into the table
                // "before" the Add row
                addRow.before(newRow);

                // add the remove function to the new row
                $('a.remove', newRow).click(function(){
                    $(this).parent().parent().remove();
                    return false;               
                });

                // prevent the default click
                return false;
            });
        });
        </script>

html如下:

<table>
<tr><td>
  Email Addresses</td><td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
  <td><input type="hidden" name="on2" value="Email Address 1"><a id="addnew" href="">Add</a></td><td><input name="os2" type="text" id="os2" value="" size="24" maxlength="60" /></td>
  <td>&nbsp;</td>
</tr>
</table>

我很困惑的是,为什么当我在浏览器中查看页面时,我看到了额外的输入字段,但当我查看源代码时,却找不到额外字段的代码。

有人可以告诉我吗?

戴夫

【问题讨论】:

    标签: jquery forms input


    【解决方案1】:

    我很困惑的是为什么,什么时候 我在浏览器中查看页面,我 看到额外的输入字段,但是当我 看源码,代码 找不到额外的字段。

    这是因为浏览器只向您显示页面加载时的 DOM。

    您需要FirebugWebdeveloper Toolbar(都是 Firefox 附加组件),然后您可以看到 jQuery 为修改您的 DOM 做了什么。

    编辑:还有一个Webdeveloper Toolbar for Internet Explorer

    【讨论】:

    • +1 用于 IE 开发工具栏,非常方便!我实际上一直在寻找类似的东西,但不知道我是怎么错过的。
    【解决方案2】:

    根据您查看源的位置,它不会在那里。 比如在IE中。它只会报告页面在任何 javascript 修改之前的状态。

    为 firefox 获取 firebug。它会让你看到一切。

    【讨论】:

      【解决方案3】:

      这些元素是动态创建的,因此在源代码中找不到。并不是说通过javascript更改源代码时会自动更新。

      一个名为 Web developers toolbar 的 firefox 扩展程序有一个选项“查看生成的源代码”,它会向您显示源代码,包括动态创建的元素。

      【讨论】:

      • 好吧,我的问题是:我正在尝试将这些动态创建的字段的值连同其适当的名称值一起发送到 PayPal,但它们根本没有被传递。像这样生成一个字段时,POST数组中没有传递值吗?戴夫
      • 他们应该是;我在我的一个应用程序中使用了这种技术。您确定这些元素实际上已附加到表单中吗?使用 firebug 之类的工具来检查。
      • 好吧,我发现我的逻辑有一些错误,使用 WebDeveloper 帮助我看到了这一点。我会在一分钟内检查 Firebug,但也许你可以帮助我解决最后一个逻辑问题:我正在使用以下内容尝试循环并设置名称和 ID,但它不起作用。我可能把 first-child.next-sibling 弄错了: $('td:first-child.next-sibling.input', newRow).each(function(i){ var newID = newRowNum; $(this). attr('id','os' + newID).attr('name','os' + newID); });戴夫
      猜你喜欢
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多