这是我的代码。我对输入元素有不同的is,我的想法是更新所有输入的相同值。我有了如何更新所有输入onchange的想法,但我需要更改下一个,而不是前一个。例如,假设我有20个输入,如果我更改了第8个输入,则值应更新为从第8个输入到第20个输入的所有输入,但前7个输入不应更改。我在这里强调了如何编写代码。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.tc').change(function(){
var id=$(this).attr("id");
var val=$(this).val();
var tot=$('.tc').length;
});
});
</script>
</head>
<body>
<?php
include('connect.php');
$yr=$_POST['year'];
$sm=$_POST['sem'];
$br=$_POST['branch'];
$k=0;
$sql="select * from intstd where year='$yr' and sem='$sm' and branch='$br'";
$query=mysql_query($sql);
echo"<table>";
while($row=mysql_fetch_array($query))
{
$k++;
echo"<tr>";
echo"<td>$k</td>";
echo"<td>". $row['rollno']."</td>";
echo"<td><input type='text' name='a[]' class='tc' id='$k' /></td>";
echo"<td><input type='text' name='b[]' id='$k' /></td>";
echo"</tr>";
}
echo"</table>";
?>
</body>
</html>发布于 2017-09-07 20:26:38
下面是一个示例解决方案https://jsfiddle.net/6mxo5zu1/
$('input[type="text"]').change(function(){
var that = this;
$(this).nextAll('input[type="text"]').each(function(){
$(this).val($(that).val());
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text-1" />
<input type="text" id="text-2" />
<input type="text" id="text-3" />
<input type="text" id="text-4" />
<input type="text" id="text-5" />
<input type="text" id="text-6" />
<input type="text" id="text-7" />
<input type="text" id="text-8" />
<input type="text" id="text-9" />
<input type="text" id="text-10" />
<input type="text" id="text-11" />
<input type="text" id="text-12" />
我有12个带有唯一id的input textbox。
其中一个textbox值发生更改,然后input textbox值的其余部分将只更新下一个,而不是前一个。
这里不需要使用id。
希望这能对你有所帮助。
发布于 2017-09-07 20:18:51
只需为您的输入设置唯一的in (比如说,以input_0、input_1等形式)试着像这样循环:
for (var i=first_index_to_modify; i<=last_index_to_modify; i++) {
$('#input_'+i).val('myval');
}发布于 2017-09-07 20:21:46
我看到您的输入具有顺序ids,如1、2、3等等。
你可以简单地这样做。
var i = 0;
for(i = 1;i <$('.tc').length; i++ ) {
if ($('.tc')[i].attr('id') >= id) {
$('.tc')[i].val(val);
}
}https://stackoverflow.com/questions/46095914
复制相似问题