2017-09-07 61 views
0

這裏是我的代碼。我對輸入元素有不同的ID,我的想法是更新所有輸入的相同值。我有想法如何更新onchange的所有輸入,但我需要改變下一個,但不是以前。示例假設如果我有20個輸入,如果我改變了第8個輸入,值應該更新爲從第8個輸入到第20個輸入的所有輸入,但前面的7個輸入不應該改變。我在這裏堅持如何編寫代碼。用不同的ID更新下一個輸入,具有相同的值

<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> 

回答

0

就(像input_0input_1等讓我們說,在形式上)設定唯一的ID爲您的投入和嘗試循環像這樣:

for (var i=first_index_to_modify; i<=last_index_to_modify; i++) { 
    $('#input_'+i).val('myval'); 
} 
0

我看到你輸入具有順序ID,如1,2,3等等。

你可以簡單地這樣做。

var i = 0; 
for(i = 1;i <$('.tc').length; i++) { 
    if ($('.tc')[i].attr('id') >= id) { 
     $('.tc')[i].val(val); 
    } 
} 
1

在這裏你去用一個例子液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 input textbox獨特id連接到它。

其中一個文本框值的變化,然後input textbox值的其餘部分將更新只有下一個不是以前的

沒有必要在這裏使用id

希望這會幫助你。

+0

其不工作 – PAVAN

+0

您的聲明「其不工作」非常模糊。你能詳細說明什麼不起作用嗎? – Shiladitya