且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在提交按钮上的javascript中更改标签颜色

更新时间:2023-09-18 23:15:04

标签在提交后立即变色,但页面立即刷新. 您可以通过检查是否单击了保存"按钮来为标签着色:

Label changes color after ckick on submit but page is refreshing immediately. You can color the label by checking if button "save" was clicked :

<?php

if($isSaveDisabled) {
  echo '<script>document.getElementById("myID").style.color = "#ff0000";</script>'
}
?>

因此,您的代码将如下所示:

Therefore your code will look:

<?php
//php code 
$isSaveDisabled=true;
$isCreateDisabled=false;

if(isset($_POST['save_button']))
{
echo 'Hello PHP';
}

if(isset($_POST['create_button']))
{
$isSaveDisabled=false;
}
?>
// BootStrap Code
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My New PHP CODE</title>

<link rel="stylesheet" href="bootStrap/css/bootstrap.css"/>
<link rel="stylesheet" href="bootStrap/css/bootstrap.min.css"/>

<script type="text/javascript">
function myFunction() {
document.getElementById("myID").style.color = "#ff0000";
}
</script>

</head>
<body>
<div class="container jumbotron text-center">

    <form action="" method="POST" class="form-inline">
        <div class="form-group">
            <label for="fname" id="myID">FIRST NAME</label>
            <input type="text" name="fname" class="form-control" >
             <label for="nlame">LAST NAME</label>
            <input type="text" name="lname" class="form-control" >
        </div>
        <br><br>
            <div class="btn-group">
            <button type="submit" name="save_button" onclick="myFunction();" <?php echo $isSaveDisabled? 'disabled':'';?>>SAVE</button>
             <button type="submit" name="create_button" <?php echo $isCreateDisabled? 'disabled':''?> >CREATE</button>

        </div>
    </form>
</div>

<?php
  if($isSaveDisabled) {
    echo '<script>document.getElementById("myID").style.color = "#ff0000";</script>';
  }
?>
</body>
</html>