如何重复相同的值直到最后一个文本框
How to repeat the same value till last textbox?
我计划将双击的文本框值重复到文本框的末尾。我的表单包含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,所以代码应该被理解为伪代码,当你只是复制粘贴它时就不起作用了。
相关文章:
- 尝试使用Javascript正则表达式来获取“&"分隔文本,无论它是否's是最后一个值
- jQuery/Javascript>on单击将文本放置在最后一个已知的光标位置
- 如何从由“”分隔的字符串中提取最后一个文本|&”;
- 为什么jQuery正在删除文本的最后一个字符
- 如何在单击按钮时获得最后一个焦点文本框输入
- 如何在文本区域中获取最后一个单词的位置
- 匹配最后一个点之后的文本字符串
- Jquery:在检查最后一个字母时将文本从表单复制到 DIV
- 仅将 Javascript 代码添加到最后一个文本区域
- JavaScript 替换第一个/最后一个字母并将中间文本括在标签中
- 如何在用户完成第一个输入文本的键入后设置和聚焦最后一个输入文本
- 删除元素文本中的最后一个字符
- 用于获取最后一个方括号 () 之间的文本的正则表达式
- JQuery 或 Javascript 代码,用于更改 html 文本中最后一个字符的字体
- SVG
文本循环获取数组的最后一个元素 - 如何使用 JS 正则表达式将文本块中的每个最后一个字母都设置为粗体
- 如何在JavaScript中更改最后一个文本节点子节点
- 脚本只会最后一个文本和最后一个颜色,但会按照指示进行动画,发生了什么?
- jQuery:当最后一个文本框被填充时创建新的文本框
- 如何重复相同的值直到最后一个文本框