如何重复相同的值直到最后一个文本框

How to repeat the same value till last textbox?

本文关键字:最后一个 文本 何重复      更新时间:2023-09-26

我计划将双击的文本框值重复到文本框的末尾。我的表单包含10个文本框,其中包含10个不同的值。如果我点击第三个文本框,第三个文字框的值应该重复到第十个文字框。但是2和第一个文本框保持相同的值。我已经在代码笔中包含了代码,请找到下面的链接。

http://codepen.io/nachicode/pen/dXKaaA/

代码段:

HTML

<table>
  <tr>
    <th>One</th>
    <th>Two</th>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="1" />
    </td>
    <td>
      <input Type="text" Value="1" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="2" />
    </td>
    <td>
      <input Type="text" Value="2" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="3" />
    </td>
    <td>
      <input Type="text" Value="3" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="4" />
    </td>
    <td>
      <input Type="text" Value="4" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="5" />
    </td>
    <td>
      <input Type="text" Value="5" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="6" />
    </td>
    <td>
      <input Type="text" Value="6" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="7" />
    </td>
    <td>
      <input Type="text" Value="7" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="8" />
    </td>
    <td>
      <input Type="text" Value="8" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="9" />
    </td>
    <td>
      <input Type="text" Value="9" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="10" />
    </td>
    <td>
      <input Type="text" Value="10" />
    </td>
  </tr>
</table>

查询

$(document).ready(function() {
  $("input").dblclick(function() {
    //Code here
  });
});

一种方法如下:

// select all <input> elements within a <td> element,
// bind the anonymous function of the on() method as
// the 'dblclick' event-handler:
$('td input').on('dblclick', function() {
  // find the ancestor <tr> or the dblclick-ed element:
  var currentRow = $(this).closest('tr'),
    // get all subsequent siblings of the current <tr>,
    // and add the current <tr> back to that selection
    // with addBack():
    laterSiblingRows = currentRow.nextAll().addBack(),
    // find relevant <input> elements within the <tr>
    // elements:
    laterInputs = laterSiblingRows.find('input');
  // update the value of those <input> elements to
  // the value of the dblclick-ed element:
  laterInputs.val(this.value);
})

$('td input').on('dblclick', function() {
  var currentRow = $(this).closest('tr'),
    laterSiblingRows = currentRow.nextAll().addBack(),
    laterInputs = laterSiblingRows.find('input');
  laterInputs.val(this.value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>One</th>
    <th>Two</th>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="1" />
    </td>
    <td>
      <input Type="text" Value="1" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="2" />
    </td>
    <td>
      <input Type="text" Value="2" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="3" />
    </td>
    <td>
      <input Type="text" Value="3" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="4" />
    </td>
    <td>
      <input Type="text" Value="4" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="5" />
    </td>
    <td>
      <input Type="text" Value="5" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="6" />
    </td>
    <td>
      <input Type="text" Value="6" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="7" />
    </td>
    <td>
      <input Type="text" Value="7" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="8" />
    </td>
    <td>
      <input Type="text" Value="8" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="9" />
    </td>
    <td>
      <input Type="text" Value="9" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="10" />
    </td>
    <td>
      <input Type="text" Value="10" />
    </td>
  </tr>
</table>

JS Fiddle。

参考文献:

  • CCD_ 1
  • CCD_ 2
  • nextAll()
  • on()
  • val()

类似的东西?

$(document).ready(function() {
  $("input").dblclick(function() {
    var index = $(this).closest('td').index() + 1;
    var rows = $(this).closest('tr').nextAll().addBack();
    rows.find('td:nth-child(' + index + ') input').val($(this).val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <th>One</th>
    <th>Two</th>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="1" />
    </td>
    <td>
      <input Type="text" Value="1" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="2" />
    </td>
    <td>
      <input Type="text" Value="2" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="3" />
    </td>
    <td>
      <input Type="text" Value="3" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="4" />
    </td>
    <td>
      <input Type="text" Value="4" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="5" />
    </td>
    <td>
      <input Type="text" Value="5" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="6" />
    </td>
    <td>
      <input Type="text" Value="6" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="7" />
    </td>
    <td>
      <input Type="text" Value="7" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="8" />
    </td>
    <td>
      <input Type="text" Value="8" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="9" />
    </td>
    <td>
      <input Type="text" Value="9" />
    </td>
  </tr>
  <tr>
    <td>
      <input Type="text" Value="10" />
    </td>
    <td>
      <input Type="text" Value="10" />
    </td>
  </tr>
</table>

试试这个:

$("input").dblclick(function(){
        var id = $(this).val();
        for(start=id; start<=10; start++)
        {
            $('input[value="'+start+'"]').val('hello');
            //$('input[value="enter highlight"]')
        }
    });

我不会在这里发布现成的代码,但你可以给你的输入id,如"box1"、"box2"等。当你双击输入时,你可以得到它的id,得到它的索引,然后增加索引,并通过"box"引用下一个输入,直到你找不到具有这样id的元素,或者达到一个特殊的数字,比如10。

一些伪代码:

var clickedBoxId = this.ID; // "this" should point to the box clicked
var index = clickedBoxId.Replace('box','');
for( int i = index; i <= 10; i++ )
{
    var anotherBox = document.getElementById('box' + i);
    if( anotherBox !== 'undefined' )
    {
        anotherBox.value = this.value
    }
}

注意,我不经常使用javascript,所以代码应该被理解为伪代码,当你只是复制粘贴它时就不起作用了。

相关文章: