使用jQuery根据所选附加项目的特定行反映基于值的部分。

huangapple go评论81阅读模式
英文:

Reflect the value based on the specific row of selected appended items using jQuery

问题

以下是您要翻译的内容:

"I have an issue in which I am trying to capture the value of an input type element on keyup and then reflect that value in a span which is the total amount of the item. I have my snippet but when I try to handle the keyup event on the input type it changes the value of the total for all selected items; I just want to change the value of the total for the row in which the keyup event was triggered and to be able to remove the specific row based on the click of the remove item button. Did I miss something? How can I get the specific row? Can anybody help me with this?"

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
$("#allcb").change(function () {
  $('tbody tr td input[type="checkbox"]').prop(
    "checked",
    $(this).prop("checked")
  );
});

$(document).on( "keyup",  ".quantity_input",function() {
  var value_input = 0;
  var price_value = 0;
  var total_value = 0;

  value_input = $(".quantity_input").val();
  price_value = $(".price_value").html();
  total_value = parseInt(value_input) * parseInt(price_value);
  $(".total_value_row").text(total_value);
});

$(document).on("click",".remove_data" , function(){
        alert("I've been clicked! remove the row item");
});

$(document).ready(function () {
  $("#add_data").click(function () {
    var grid = document.getElementById("Table1");
    var message = "Values                 Description\n";

    var checkBoxes = grid.getElementsByTagName("INPUT");
    var str = '';

    for (var i = 0; i < checkBoxes.length; i++) {
      if (
        checkBoxes[i].checked &&
        checkBoxes[i].name !== "allcb" &&
        !checkBoxes[i].hasCovered
      ) {
        var row = checkBoxes[i].parentNode.parentNode;
        str +=
          '<div class="row" ><div class="col-md-8"><p class="me-3"> ' +
          '<a href="#" class="text-body">' +
          '<button type="button" class = "remove_data" id="remove_data">Remove item</button></a>&nbsp' +

          '<span>Quantity</span>&nbsp' +
          row.cells[2].innerHTML +
          "</a></p> " +
          '<span class="me-1">Price </span>&nbsp' +
          '<span class="me-1 price_value">' +
          row.cells[1].innerHTML +
          "</span>&nbsp" +
          '<input type="number" id="quantity_input" class="form-control form-control-sm w-px-75 quantity_input" value="1" min="1" max="5"/>&nbsp'+
          '<span class= "total_value_row" >total value is</span> '+
          "</div></div><hr>";
          checkBoxes[i].hasCovered = true;
        }
      }
      $(".whole-div-class").append(str);
  });
});

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table border="1" id="Table1">
  <thead>
    <tr>
      <th>
        <input type="checkbox" id="allcb" name="allcb" />
      </th>
      <th>Price</th>
      <td>Quantity</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="checkbox" id="cb1" name="cb1" />
      </td>
      <td>200</td>
      <td> 25</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" id="cb2" name="cb2" />
      </td>
      <td>300</td>
      <td>30</td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" id="cb3" name="cb3" />
      </td>
      <td>400</td>
      <td>50</td>
    </tr>
  </tbody>
</table>
<br />
<button type="button" id="add_data">Add data</button>
<hr>

<div class="row whole-div-class"></div>

<!-- end snippet -->
英文:

I have an issue in which I am trying to capture the value of an input type element on keyup and then reflect that value in a span which is the total amount of the item. I have my snippet but when I try to handle the keyup event on the input type it changes the value of the total for all selected items; I just want to change the value of the total for the row in which the keyup event was triggered and to be able to remove the specific row based on the click of the remove item button. Did I miss something? How can I get the specific row? Can anybody help me with this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#allcb&quot;).change(function () {
$(&#39;tbody tr td input[type=&quot;checkbox&quot;]&#39;).prop(
&quot;checked&quot;,
$(this).prop(&quot;checked&quot;)
);
});
$(document).on( &quot;keyup&quot;,  &quot;.quantity_input&quot;,function() {
var value_input = 0;
var price_value = 0;
var total_value = 0;
value_input = $(&quot;.quantity_input&quot;).val();
price_value = $(&quot;.price_value&quot;).html();
total_value = parseInt(value_input) * parseInt(price_value);
$(&quot;.total_value_row&quot;).text(total_value);
});
$(document).on(&quot;click&quot;,&quot;.remove_data&quot; , function(){
alert(&quot;I&#39;ve been clicked! remove the row item&quot;);
});
$(document).ready(function () {
$(&quot;#add_data&quot;).click(function () {
var grid = document.getElementById(&quot;Table1&quot;);
var message = &quot;Values                 Description\n&quot;;
var checkBoxes = grid.getElementsByTagName(&quot;INPUT&quot;);
var str = &#39;&#39;
for (var i = 0; i &lt; checkBoxes.length; i++) {
if (
checkBoxes[i].checked &amp;&amp;
checkBoxes[i].name !== &quot;allcb&quot; &amp;&amp;
!checkBoxes[i].hasCovered
) {
var row = checkBoxes[i].parentNode.parentNode;
str +=
&#39;&lt;div class=&quot;row&quot; &gt;&lt;div class=&quot;col-md-8&quot;&gt;&lt;p class=&quot;me-3&quot;&gt; &#39; +
&#39;&lt;a href=&quot;#&quot; class=&quot;text-body&quot;&gt;&#39; +
&#39;&lt;button type=&quot;button&quot; class = &quot;remove_data&quot; id=&quot;remove_data&quot;&gt;Remove item&lt;/button&gt;&lt;/a&gt;&amp;nbsp&#39; +
&#39;&lt;span&gt;Quantity&lt;/span&gt;&amp;nbsp&#39; +
row.cells[2].innerHTML +
&quot;&lt;/a&gt;&lt;/p&gt; &quot; +
&#39;&lt;span class=&quot;me-1&quot;&gt;Price &lt;/span&gt;&amp;nbsp&#39; +
&#39;&lt;span class=&quot;me-1 price_value&quot;&gt;&#39; +
row.cells[1].innerHTML +
&quot;&lt;/span&gt;&amp;nbsp&quot; +
&#39;&lt;input type=&quot;number&quot; id=&quot;quantity_input&quot; class=&quot;form-control form-control-sm w-px-75 quantity_input&quot; value=&quot;1&quot; min=&quot;1&quot; max=&quot;5&quot;/&gt;&amp;nbsp&#39;+
&#39;&lt;span class= &quot;total_value_row&quot; &gt;total value is&lt;/span&gt; &#39;+
&quot;&lt;/div&gt;&lt;/div&gt;&lt;hr&gt;&quot;;
checkBoxes[i].hasCovered = true;
}
}
$(&quot;.whole-div-class&quot;).append(str);
});
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table border=&quot;1&quot; id=&quot;Table1&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;allcb&quot; name=&quot;allcb&quot; /&gt;
&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;
&lt;td&gt;Quantity&lt;/td&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;cb1&quot; name=&quot;cb1&quot; /&gt;
&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt; 25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;cb2&quot; name=&quot;cb2&quot; /&gt;
&lt;/td&gt;
&lt;td&gt;300&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;cb3&quot; name=&quot;cb3&quot; /&gt;
&lt;/td&gt;
&lt;td&gt;400&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;button type=&quot;button&quot; id=&quot;add_data&quot;&gt;Add data&lt;/button&gt;
&lt;hr&gt;
&lt;div class=&quot;row whole-div-class&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

.quantity_input元素发生keyup事件时,我们希望仅更改元素所在行中的值的状态。为实现此目的,我们需要将状态操纵的影响限定到包含该元素的.row。我们可以使用jQuery的.closest方法获取.row,然后将结果传递给处理程序中的所有jQuery选择器调用,以始终将结果集限制为.row

$(document).on("keyup", ".quantity_input", function() {
  var $row = $(this).closest('.row');
  var value_input = 0;
  var price_value = 0;
  var total_value = 0;

  value_input = $(".quantity_input", $row).val();
  price_value = $(".price_value", $row).html();
  total_value = parseInt(value_input) * parseInt(price_value);
  $(".total_value_row", $row).text(total_value);
});

对于移除处理程序,我们可以采取相同的方法。获取最接近被点击的.remove_data按钮的.row,然后对其调用jQuery的.remove

$(document).on("click", ".remove_data", function() {
  var $row = $(this).closest('.row');
  
  $row.remove();
});

请注意:我还建议将您的<hr>作为.row的兄弟元素移至.row内,以便在移除每个.row时不会保留大量<hr>元素。

要重置与已移除行对应的复选框,我们需要一种方法来获取与该行对应的复选框。其中一种方法是使用data属性。我们可以在每一行上设置一个包含其关联复选框名称的data属性。然后,在移除行时,我们可以使用该data属性获取复选框,取消选中它,并将其hasCovered属性设置为false

$(document).on("click", ".remove_data", function() {
  var $row = $(this).closest('.row');
  var checkboxName = $row.data('for');
  var $checkbox = $(`input[type="checkbox"][name="${checkboxName}"]`);
  
  $row.remove();
  $checkbox.prop('checked', false);
  $checkbox.get(0).hasCovered = false;
});

完整的代码示例如下:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1" id="Table1">
  <thead>
    <tr>
      <th>
        <input type="checkbox" id="allcb" name="allcb" />
      </th>
      <th>Price</th>
      <td>Quantity</td>
    </tr>
  </thead>
  <tbody>
    <!-- ... -->
  </tbody>
</table>
<br />
<button type="button" id="add_data">Add data</button>
<hr>

<div class="row whole-div-class"></div>
英文:

When there is a keyup event on an .quantity_input element, we want only the state of the values in the element's row to change. To achieve this, we need to scope the effects of our state manipulation to the .row that contains the element. We can get the the .row by using jQuery's .closest method and then passing the result to all of the jQuery selector calls in the handler so that the result set will always be constrained to the .row.

$(document).on(&quot;keyup&quot;, &quot;.quantity_input&quot;, function() {
var $row = $(this).closest(&#39;.row&#39;);
var value_input = 0;
var price_value = 0;
var total_value = 0;
value_input = $(&quot;.quantity_input&quot;, $row).val();
price_value = $(&quot;.price_value&quot;, $row).html();
total_value = parseInt(value_input) * parseInt(price_value);
$(&quot;.total_value_row&quot;, $row).text(total_value);
});

We can take the same approach for the remove handler. Get the .row that is closest to the clicked .remove_data button and call jQuery's .remove on it.

$(document).on(&quot;click&quot;, &quot;.remove_data&quot;, function() {
var $row = $(this).closest(&#39;.row&#39;);
$row.remove();
});

Note: I would also recommend moving the &lt;hr&gt; that you have as a sibling of .row to be contained by .row, so that you don't get a bunch of &lt;hr&gt; elements remaining as you remove each .row.

In order to reset the checkbox that corresponds to the row that was removed, we would need a way to get the checkbox that corresponds to the row. One way to do this would be with a data-attribute. We could set a data-attribute on each row containing the name of its associated checkbox. Then, when the row is removed, we can use that data-attribute to get the checkbox, uncheck it, and set its hasCovered property to false.

$(document).on(&quot;click&quot;, &quot;.remove_data&quot;, function() {
var $row = $(this).closest(&#39;.row&#39;);
var checkboxName = $row.data(&#39;for&#39;);
var $checkbox = $(`input[type=&quot;checkbox&quot;][name=&quot;${checkboxName}&quot;]`);
$row.remove();
$checkbox.prop(&#39;checked&#39;, false);
$checkbox.get(0).hasCovered = false;
});

The full code example becomes:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#allcb&quot;).change(function() {
$(&#39;tbody tr td input[type=&quot;checkbox&quot;]&#39;).prop(
&quot;checked&quot;,
$(this).prop(&quot;checked&quot;)
);
});
$(document).on(&quot;keyup&quot;, &quot;.quantity_input&quot;, function() {
var $row = $(this).closest(&#39;.row&#39;);
var value_input = 0;
var price_value = 0;
var total_value = 0;
value_input = $(&quot;.quantity_input&quot;, $row).val();
price_value = $(&quot;.price_value&quot;, $row).html();
total_value = parseInt(value_input) * parseInt(price_value);
$(&quot;.total_value_row&quot;, $row).text(total_value);
});
$(document).on(&quot;click&quot;, &quot;.remove_data&quot;, function() {
var $row = $(this).closest(&#39;.row&#39;);
var checkboxName = $row.data(&#39;for&#39;);
var $checkbox = $(`input[type=&quot;checkbox&quot;][name=&quot;${checkboxName}&quot;]`);
$row.remove();
$checkbox.prop(&#39;checked&#39;, false);
$checkbox.get(0).hasCovered = false;
});
$(document).ready(function() {
$(&quot;#add_data&quot;).click(function() {
var grid = document.getElementById(&quot;Table1&quot;);
var message = &quot;Values                 Description\n&quot;;
var checkBoxes = grid.getElementsByTagName(&quot;INPUT&quot;);
var str = &#39;&#39;
for (var i = 0; i &lt; checkBoxes.length; i++) {
if (
checkBoxes[i].checked &amp;&amp;
checkBoxes[i].name !== &quot;allcb&quot; &amp;&amp;
!checkBoxes[i].hasCovered
) {
var row = checkBoxes[i].parentNode.parentNode;
str +=
&#39;&lt;div class=&quot;row&quot; data-for=&quot;&#39; + checkBoxes[i].id + &#39;&quot;&gt;&lt;div class=&quot;col-md-8&quot;&gt;&lt;p class=&quot;me-3&quot;&gt; &#39; +
&#39;&lt;a href=&quot;#&quot; class=&quot;text-body&quot;&gt;&#39; +
&#39;&lt;button type=&quot;button&quot; class = &quot;remove_data&quot; id=&quot;remove_data&quot;&gt;Remove item&lt;/button&gt;&lt;/a&gt;&amp;nbsp&#39; +
&#39;&lt;span&gt;Quantity&lt;/span&gt;&amp;nbsp&#39; +
row.cells[2].innerHTML +
&quot;&lt;/a&gt;&lt;/p&gt; &quot; +
&#39;&lt;span class=&quot;me-1&quot;&gt;Price &lt;/span&gt;&amp;nbsp&#39; +
&#39;&lt;span class=&quot;me-1 price_value&quot;&gt;&#39; +
row.cells[1].innerHTML +
&quot;&lt;/span&gt;&amp;nbsp&quot; +
&#39;&lt;input type=&quot;number&quot; id=&quot;quantity_input&quot; class=&quot;form-control form-control-sm w-px-75 quantity_input&quot; value=&quot;1&quot; min=&quot;1&quot; max=&quot;5&quot;/&gt;&amp;nbsp&#39; +
&#39;&lt;span class= &quot;total_value_row&quot; &gt;total value is&lt;/span&gt; &#39; +
&quot;&lt;/div&gt;&lt;hr&gt;&lt;/div&gt;&quot;;
checkBoxes[i].hasCovered = true;
}
}
$(&quot;.whole-div-class&quot;).append(str);
});
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table border=&quot;1&quot; id=&quot;Table1&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;allcb&quot; name=&quot;allcb&quot; /&gt;
&lt;/th&gt;
&lt;th&gt;Price&lt;/th&gt;
&lt;td&gt;Quantity&lt;/td&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;cb1&quot; name=&quot;cb1&quot; /&gt;
&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt; 25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;cb2&quot; name=&quot;cb2&quot; /&gt;
&lt;/td&gt;
&lt;td&gt;300&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;cb3&quot; name=&quot;cb3&quot; /&gt;
&lt;/td&gt;
&lt;td&gt;400&lt;/td&gt;
&lt;td&gt;50&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br /&gt;
&lt;button type=&quot;button&quot; id=&quot;add_data&quot;&gt;Add data&lt;/button&gt;
&lt;hr&gt;
&lt;div class=&quot;row whole-div-class&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月6日 21:44:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定