【问题标题】:Jquery Select all things in an element with a certain name and replaceJquery选择具有特定名称的元素中的所有事物并替换
【发布时间】:2014-11-21 21:10:41
【问题描述】:

目前我有一个用于提交的表格。这是我正在使用的代码块的一小段摘录。

<tr class="draggable even">
  <td class="choice-flag">
    <a title="Drag to re-order" href="#" class="tabledrag-handle">
      <div class="handle">&nbsp;</div>
    </a>
  </td>
  <td>
    <input name="recipe_ingredients[ing][1][ri_id]" data-thmr="thmr_141" type="hidden">
    <div class="form-item form-type-textfield form-item-recipe-ingredients-ing-1-quantity"     data-thmr="thmr_143">
      <label for="edit-recipe-ingredients-ing-1-quantity" data-thmr="thmr_144">Quantity</label>
      <input class="form-item-recipe-ingredients-quantity form-text" id="edit-recipe-ingredients-ing-1-quantity" name="recipe_ingredients[ing][1][quantity]" value="" size="8" maxlength="8" data-thmr="thmr_142" type="text">
    </div>
  </td>
  <td>
    <div class="form-item form-type-select form-item-recipe-ingredients-ing-1-unit-key" data-thmr="thmr_146">
      <label for="edit-recipe-ingredients-ing-1-unit-key" data-thmr="thmr_147">Unit</label>
      <select class="form-item-recipe-ingredients-unit-key form-select" id="edit-recipe-ingredients-ing-1-unit-key" name="recipe_ingredients[ing][1][unit_key]" data-thmr="thmr_145">

现在的问题是,每一行在某些属性的名称中都有一个唯一的 1、2、3、4、5 标识符。单击按钮时,我希望它添加下一行,这需要更改名称,例如

edit-recipe-ingredients-ing-1-unit-key   

edit-recipe-ingredients-ing-2-unit-key

有没有一种方法可以选择和更改元素中与字符串匹配的 all 属性,并用我的新名称替换每个出现的属性?我计划使用 jquery clone 命令添加新行。

感谢您的帮助!

【问题讨论】:

标签: javascript jquery


【解决方案1】:

您可以克隆您的行,然后使用 RegExp 将其更改为内部 HTML。

但实际上你应该在这里有某种模板。请考虑使用 Handlebars 或类似的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    相关资源
    最近更新 更多