简体   繁体   中英

How to change css using jquery in a php loop

I want to change the css of my textbox and to disabled the submit button in a php loop if it meets certain condiion .

I've successfully manage to loop the data through php but only the first textbox is changed

I'll put a few part of the php codes as php part is already done succesfully

Script

<style type="text/css">
.first
{
  color:#0C0
}
.second
{
   color:#F00
}

</style>

 <script src="js/jquery-1.8.3.js"></script>
 <script src="js/jquery-ui.js" > </script>
 <script> var $j = jQuery.noConflict();

 function changeClass(str)
 {
      if ($j('#status').val()== "No")
      {
          $j('#status').addClass('first');
      }
  else
  {
         $j('#status').removeClass('first');                                
         $j('#status').addClass('second');                   
         $j('#submit').prop('disabled', true);
       }              
return true;
 }
  </script>

HTML

 <table width="800" border="0" align="center" bordercolor="#000000" 
          onmousemove="changeClass(this);"> 
 <?php do { ?>
     <tr bgcolor="#FFFFCC">
       <th  align="center" bgcolor="#3366FF" class="style68" >Status</th>
       <th  align="center" bgcolor="#3366FF" class="style68" >Submit</th>
       <td  align="center" bgcolor="#CCCCCC">
          <form  > <input type="text"  id="status" name="status" 
           value="<?php echo$data['status']; ?>" >
       </td> 
       <td  align="center" bgcolor="#CCCCCC">
         <button id="submit" name="submit" type="submit" > Submit</button>
    </form>
   </td>
         </td>
                 </tr>   
                  <?php } while ($data = mysql_fetch_assoc($re)); ?>
                </table>
  $(document).ready(function(){
  $('table').hover(function(){
    $( ".changeClass" ).each(function() {
    if($(this).val()=='No')
    {
    $(this).addClass( "second" );
    $(this).parent().parent().find('.submitClass').prop('disabled', true);
    }
    else
    {
    $(this).addClass( "first" );
    }
    });
    return true;
    })
});



  <input type="text" class="changeClass"  name="status"  value="<?php echo$data['status']; ?>" >

Elements id in HTML must be unique. You should use the table id and go through the tables td children to do it. Or you can use the td class status and call them .status instead of #status in jquery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM