【问题标题】:how to change label color in javascript on submit button如何在提交按钮上更改javascript中的标签颜色
【发布时间】:2016-12-13 08:35:53
【问题描述】:

你好朋友,我的代码有问题, 在这段代码中,我希望当我按下保存按钮然后“Hello PHP”打印和名字的标签颜色改变但它不起作用,当我将提交输入按钮类型时,它显示“Hello PHP”并且标签颜色没有改变,当我按钮类型中的按钮然后标签颜色更改但不打印'hello PHP',我想要保存按钮单击,请帮助我摆脱这段代码

这是我的代码:

<?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>
</body>
</html>

【问题讨论】:

    标签: jquery html twitter-bootstrap


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 非常感谢兄弟,对我很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2014-04-16
    • 2019-03-22
    • 2014-10-03
    相关资源
    最近更新 更多