简体   繁体   中英

Changing a Table Row's colour onload

So I'm working on a webpage where an admin can disable and enable users. I have it so when the admin disables or enables a user, the text in the user's table row will change colour accordingly. However, I can't seem to change the row's colour onload. So they all show up as the enabled colour (Black). Here's my code so far;

function loadRow(){


    var check_array = document.getElementsByName("checkbox[]");
    var array_size = check_array.length;

    for(var i =0; i < array_size; i++){
        if(check_array[i].checked){
            var rowId = check_array[i].value;
            document.getElementById(rowId).style.color = "#4D4A49"; 
        }
    }

}

My rows are all populated through a PHP connection to my database, and here's the code for that;

while($row = mysqli_fetch_array($result)){
    $output .= '<tr id="row'. $row['id'] .'"><td>' . $row['name'] .'</td>
                <td>'. $row['username'] .'</td>
                <td>' . $row['email'] . '</td>
                <td><input name="checkbox[]" type="checkbox" id="checkbox'. $row['id'] .'"  value="' . $row['id'] . '" onchange="disableUser(this.value)" ' . ($row['disable'] == 0 ? 'checked' : '') .'/></td>
                <td><a href="deleteUser.php">Delete</a></td>
                </tr>';
}

The HTML then looks like this;

<table border="1">
    <tr><th>Name</th><th>Username</th><th>Email</th><th>Disabled</th><th>Delete</th></tr>
    <?php echo $output; ?>
</table>

I am wondering if the onload is in the wrong place or not. Any suggestions? If I left something out, and am unclear, please let me know and I'll edit my post. Thanks.

You have used:

'<tr id="row'. $row['id'] .'">'

but

var rowId = check_array[i].value;
document.getElementById(rowId).style.color = "#4D4A49"; 

is using the value attribute from the checkboxes which are set to:

value="' . $row['id'] . '"

hence either change:

document.getElementById('row' + rowId).style.color = "#4D4A49"; 

or

'<input name="checkbox[]" type="checkbox" id="checkbox'. $row['id'] .'"  value="row' . $row['id'] . '" onchange="disableUser(this.value)" ' . ($row['disable'] == 0 ? 'checked' : '') .'/>' 

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