简体   繁体   English

为什么我的Javascript全局变量仅更新一次? jQuery的最后一个选择器错误?

[英]Why do my Javascript global variables only update once? jQuery last-child selector error?

I'm trying to learn jQuery and testing out ideas on JS Fiddle and I seem to be having a brain freeze. 我正在尝试学习jQuery并在JS Fiddle上测试想法,而我似乎脑子僵硬了。

The code below should display a row of a table that has a "+" at the end to add new rows. 下面的代码应显示表的一行,该表的末尾带有“ +”以添加新行。 The first time I click, it properly assigns the new IDs to the new row (and resets values to a virgin state in the new row, changes the old row's "+" to a "-"). 第一次单击时,它将为新行正确分配新ID(并将新行中的值重置为原始状态,将旧行的“ +”更改为“-”)。 I'm not understanding why the values of oldID and newID don't continue to increment after the first run through the code. 我不明白为什么在第一次运行代码后,oldID和newID的值不会继续增加。

It reads "32" as the numeric portion of the old last child, and then creates a new row with "33" appending it to the table but after that recomputing the id using the last child selector doesn't appear to actually pick the NEW last child. 它读取“ 32”作为旧的最后一个子代的数字部分,然后创建一个新行,并在表中附加“ 33”,但是在使用最后一个子选择器重新计算id之后,似乎并没有实际选择NEW最后一个孩子。

While suggestions about how to make the code better are welcome, I really want to know specifically what I'm missing in the current code, inelegant as it may be. 虽然欢迎提出有关如何使代码更好的建议,但我确实想特别了解我当前代码中缺少的内容,尽管可能不太好。

Thanks! 谢谢!

<table>

<tbody id="investigators">
<tr id="investigator.32">

  <td>
    <input name="person.32" id="person.32"
           type="text" value=""
           placeholder="Lastname, Firstname" 
           onfocus="this.select();" />
  </td>

  <td style="text-align:center" id="add_more.32">
    <img src="add_green.svg"
         alt="[+]" title="Add investigator"
         style="height:1em; width:1em;" />
  </td>

</tr>
</tbody>

And Javascript / jQuery code: 和Javascript / jQuery代码:

var oldID = parseInt($("#investigators :last-child")
                       .attr("id").split(".")[1]);
var newID = oldID+1;
$("#add_more\\."+oldID).click(function(){
  var $newPI = $("#investigator\\."+oldID).clone(true);
  $newPI.find("#person\\."+oldID)
    .val(null)
    .attr("placeholder","Jungle, George of")
    .attr("name","person."+newID)
    .attr("id",  "person."+newID);
  $newPI.find("#add_more\\."+oldID)
    .attr("id","add_more."+newID);
  $newPI
    .attr("id","investigator."+newID);
  $newPI.appendTo("#investigators");
  $("#add_more\\."+oldID+" :first-child")
    .attr("src","delete_red.svg")
    .attr("alt","[X]")
    .attr("title","Remove investigator");
  oldID = parseInt($("#investigators :last-child")
                       .attr("id").split(".")[1]);
  newID = oldID+1;
});

I think your problem is that you're never binding to click on the newly created person. 我认为您的问题是,您永远不会约束click新创建的人。 Calling $("#add_more\\\\."+oldID).click(...) will bind to the original item, but not to new items as oldID changes. 调用$("#add_more\\\\."+oldID).click(...)将绑定到原始项目,但不会绑定到新项目,因为oldID更改。

Instead, I think you should be putting classes onto your elements: 相反,我认为您应该将类​​放到您的元素上:

<td>
<input name="person.32" id="person.32"
       class="person" <!-- the important line -->
       type="text" value=""
       placeholder="Lastname, Firstname" 
       onfocus="this.select();" />
</td>

Then, you can use something like jQuery.on : 然后,您可以使用类似jQuery.on的方法

$(document).on("click", "person", function() {
    // Check that this is the last person here
});

That way, every time you add a new person, you won't have to bind a new handler, it will already work. 这样,每次添加新人员时,您都不必绑定新的处理程序,该处理程序已经可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM