【问题标题】:How do I update the jQuery find selector if I make the input form field name an array? [duplicate]如果我将输入表单字段名称设为数组,如何更新 jQuery 查找选择器? [复制]
【发布时间】:2016-09-28 19:07:48
【问题描述】:

我正在重新利用一些代码来动态添加表单字段。所以我有这个:

<input name="projDesc" />

并使用以下内容创建附加字段:

function GetHtml() {

    var len = $('.extraProject').length;
    var $html = $('.extraProjectTemplate').clone();

    $html.find('[name=projDesc]')[0].name="projDesc" + len;

    return $html.html();    

}

但现在我需要将输入切换为数组供 PHP 处理:

<input name="projDesc[]" />

我现在如何更改 .find 方法以适应这种情况?谢谢。

跟进:

很抱歉受骗,但它在Fiddle 中有效但在我的网站上无效的一些可能原因是什么?现在在我的网站上,我什至没有显示初始表单字段。

在我提到 projDesc 的两种情况下,我是否还需要转义方括号?

$html.find('[name=projDesc\\[\\]]')[0].name="projDesc\\[\\]" + len;

【问题讨论】:

    标签: jquery


    【解决方案1】:

    在选择器中使用它:

    ...
    $html.find('[name=projDesc\\[\\]]')[0].name = 'projDesc' + len;
    ...
    

    答案在这里:jQuery selector for inputs with square brackets in the name attribute

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多