jquery - 找到最接近的输入值并复制到 jquery 列中的其余单元格

标签 jquery

我正在尝试获取其中有值的最近输入框的值,并将这些值复制到该列中的其余单元格。例如,如果用户单击第一列的复选框,并且该列中的任何行具有值,则必须仅将该值复制到该列中的其余单元格。 到目前为止我已经尝试过了

$("input[type='checkbox']").change(function () {
 if (this.checked) {
    var id = $(this).attr('id');
    $('.first').siblings('td').find("input").each(function () {
        var valStr=$(this).val;
        if(valStr!='')
{
    //I need to fill rest of the input boxes in this column with the value of the input box that has value
}
     });
    }
    });
         <table>
               <tr>
                   <th id="one" class="first">One <input type=checkbox id=chkOne></th>
                   <th id="two" class="sec">Two <input type=checkbox id=chkTwo></th>
                   <th id="three" class="third">Three <input type=checkbox id=chkThree></th>
               </tr>
               <tr>
                   <td class="first"><input type=text value="" /></td>
                   <td class="sec"><input type=text value="" /></td>
                   <td class="third"><input type=text value="" /></td>
               </tr>
               <tr>
                   <td class="first"><input type=text value="" /></td>
                   <td class="sec"><input type=text value="" /></td>
                   <td class="third"><input type=text value="" /></td>
               </tr>
           </table>

最佳答案

我已经修改了您的示例,以达到我认为您想要的效果: JSFIDDLE DEMO

$("input[type='checkbox']").change(function() {
    if (this.checked) {
        var id = $(this).attr('id');
        //determine parentClass because we need it to figure out which column we are working with
        var parentClass = $('#' + id).parent('th').attr('class');
        //create a copy variable that we will store the value to fill the column with in
        var copyStr = "";
        $('.' + parentClass).find("input[type='text']").each(function() {
            var valStr = $(this).val();
            if (valStr != '') {
                //store the value of the column in the copy string
                copyStr = valStr;
                //this exits the loop because we found a value to copy
                //if multiple values exist in a column it copies just the first one (but it will not overwrite other values because of the if statement in the next .each() loop)
                return false;
            }
        });
        //assign all cells in this column the value in the copy variable
        $('.' + parentClass).find("input[type='text']").each(function() {
            //check if cell has a value, if it does, do not overwrite it
            if($(this).val() == '') {
              $(this).val(copyStr);
            }
        });
    }
});

关于jquery - 找到最接近的输入值并复制到 jquery 列中的其余单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28375253/

相关文章:

javascript - 如何大写 Javascript 对象键?

javascript - 为什么我的背景图片这么大而且拉长了?

javascript - 对齐打印页面底部的div

javascript - Bootstrap 按钮动态添加,呈现方式与标记中声明的按钮不同

javascript - 如何实现 Ben Alman 的 debounce jQuery?

Jquery 灯箱不显示

javascript - jQuery first() 未按预期工作 - 返回容器

jquery - SQL 日期格式显示空白结果(使用 jQuery,例如 00-00-0000)

javascript - 禁用从 HTML 页面拖动图像

javascript - 获取表内每个表的数据内容