简体   繁体   English

如果在 javascript 的表格行上选中复选框,则更新输入

[英]Update an input if checkbox is selected on table row in javascript

I have a table where each row has a checkbox and an input.我有一个表格,其中每一行都有一个复选框和一个输入。 The input can take a numerical value.输入可以采用数值。 To make things easy for the user, there is a separate input box that a user can enter a value, click a button which sets the input values of the selected rows in the table.为了方便用户,有一个单独的输入框,用户可以输入一个值,单击一个按钮,设置表格中所选行的输入值。

[ ] all  |  Name         | Amount
[ ]      | Marty         | [       ]
[x]      | Frank         | [       ]
[ ]      | Leslie        | [       ]

[ 123.45  ] (Set selected to full amount)

When clicking the button "Set selected to full amount", the amount input for Frank would get set to 123.45.当单击“设置为全部金额”按钮时,Frank 输入的金额将设置为 123.45。

html: html:

<table>
  <thead>
  <tr>
    <th data-qaid='checkbox'><input type='checkbox'></th>
    <th data-qaid='name'>Name</th>
    <th data-qaid='amount'>Amount</th>
  </tr>
  </thead>
  <tbody>
  <tr data-qaid='datarow'>
    <td data-qaid='checkbox'>
      <input type='checkbox' name='selected' />
    </td>
    <td data-qaid='name'>
      <label>Marty</label>
    </td>
    <td data-qaid='amount'>
      <input type='number' name='amount' min='0' step='0.01' />
    </td>
  </tr>
  ...
  </tbody>
</table>
<input id='fullAmount' type='number' min='0' step='0.01' />
<button id='setFull'>Set selected to full amount</button>

javascript: This is the part where I need help with. javascript:这是我需要帮助的部分。 I'm not sure how to determine if the checkbox is selected.我不确定如何确定复选框是否被选中。

$(document).ready(function() {
  $('#setFull').onclick(function(e) {
    e.preventDefault();
    let fullAmount = $('#fullAmount').value();
    $('tr[data-qaid="datarow"]').each(function() {
      if ($(this).find('td[data-qaid="checkbox"] input').checked) {
        $(this).find('td[data-qaid="amount"] input').value = $fullAmount;
      }
    });
  });
});

I've got my attempt here: https://jsfiddle.net/cru1apo3/我在这里尝试过: https://jsfiddle.net/cru1apo3/

I have tried this code and it works:我已经尝试过这段代码并且它有效:

$(document).ready(function() {
    $('#setFull').click(function(e) {
        let fullAmount = $('#setAmount').val();
        $('tr[data-qaid="datarow"]').each(function() {
            if ($(this).find('td[data-qaid="checkbox"] input:checked').length) {
                $(this).find('td[data-qaid="amount"] input').val(fullAmount);
            }
        });
    });
});

Please, bare in mind this code only works if you put every checkboxes into a new tr tag, like this example:请记住,仅当您将每个复选框都放入新的 tr 标签时,此代码才有效,例如以下示例:

<table>
    <thead>
        <tr>
            <th data-qaid='checkbox'><input type='checkbox'></th>
            <th data-qaid='name'>Name</th>
            <th data-qaid='amount'>Amount</th>
        </tr>
    </thead>
    <tbody>
        <tr data-qaid='datarow'>
            <td data-qaid='checkbox'>
                <input type='checkbox' name='selected' />
            </td>
            <td data-qaid='name'>
                <label>Marty</label>
            </td>
            <td data-qaid='amount'>
                 <input type='number' name='amount' min='0' step='0.01' />
            </td>
        </tr>
        <tr data-qaid='datarow'>
            <td data-qaid='checkbox'>
                <input type='checkbox' name='selected' />
            </td>
            <td data-qaid='name'>
                <label>Mary</label>
            </td>
            <td data-qaid='amount'>
                <input type='number' name='amount' min='0' step='0.01' />
            </td>
        </tr>
    </tbody>
</table>

I just quickly inspected your code you have some JavaScript errors, it would be cool if you could inspect them yourself and fix them, but anyway I'm going to explain to you a few things here, so here is the your fiddle that I edited: https://jsfiddle.net/fcqdn3su/1/我只是快速检查了您的代码,您有一些 JavaScript 错误,如果您可以自己检查并修复它们会很酷,但无论如何我要在这里向您解释一些事情,所以这是我编辑的小提琴: https://jsfiddle.net/fcqdn3su/1/

  1. It looks like you were using jquery methods, but at the same time somehow forgot or confused vanilla DOM operations and jQuery methods.看起来您正在使用 jquery 方法,但同时不知何故忘记或混淆了 vanilla DOM 操作和 jQuery 方法。 So in this case:所以在这种情况下:

    $('#setFull').onclick(function(e) { $('#setFull').onclick(function(e) {

That line of code was not valid because onclick is not a jQuery method, of course in is built-in property of DOM element, so I changed onclick to click, you can also use on('click') syntax, if you want.那行代码是无效的,因为onclick不是 jQuery 方法,当然是 DOM 元素的内置属性,所以我更改了 onclick 到点击,'如果你想'也可以使用'语法。

  1. Also you check if checkbox is checked on each row, it should work fine in vanilla JavaScript but since you use jQuery selectors you need to use different syntax, there are a few ways to do it, so take a look at the jQuery docs about attr, prop or:checked properties, you will learn it quite easily.您还检查是否检查了每一行上的复选框,它应该在原版 JavaScript 中正常工作,但由于您使用 jQuery 选择器,您需要使用不同的语法,有几种方法可以做到这一点,所以看看 ZF590B4FDAC32C30BE28BZDD3C8 关于 Ctr32AF50BE28BZDD3C8 , prop or:checked properties,你会很容易学会。

  2. Also when you assign or retrieve values, you need to use jQuery val method, not default value property.此外,当您分配或检索值时,您需要使用 jQuery val方法,而不是默认属性。 so fullAmount = $('#fullAmount').value();所以 fullAmount = $('#fullAmount').value(); becomes $('#fullAmount').val();变成 $('#fullAmount').val(); and also assigning amount should be like this: $(this).find('td[data-qaid="amount"] input').val(fullAmount);并且分配金额应该是这样的: $(this).find('td[data-qaid="amount"] input').val(fullAmount);

After all the changes JavaScript code looks like this:在所有更改后 JavaScript 代码如下所示:

$(document).ready(function() {
  $('#setFull').click(function(e) {
    e.preventDefault();
    const fullAmount = $('#fullAmount').val();
    $('tr[data-qaid="datarow"]').each(function() {
      if ($(this).find('td[data-qaid="selected"] input[type=checkbox]:checked').length) {
        $(this).find('td[data-qaid="amount"] input').val(fullAmount);
      }
    })
  })
})

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

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