如何在javascript中更改标签颜色

时间:2022-11-04 21:08:08

I am problem in this javascript code, when I want to change color of label then it' not working but when I use return false after function in button it's work, This is button code onclick="myFunction(); return false">Save

我在这个javascript代码中有问题,当我想改变标签的颜色然后它'不工作但是当我在按钮中的函数使用return false之后它的工作,这是按钮代码onclick =“myFunction(); return false”> Save

but the same code return false is work but disabled button not open onclick="myFunction();">Save where I am wrong, Please Help, Thanks in Asvance

但相同的代码返回false是工作但禁用按钮未打开onclick =“myFunction();”>保存我错了,请帮助,谢谢Asvance

<?php
$isSaveDisabled = true;
$isCreateDisabled=false;
if(isset($_POST['save']))
    {
        echo 'Hello robin';
        $isCreateDisabled=false;
    }
if(isset($_POST['create']))
    {
        echo 'Byeeee robin';
        $isSaveDisabled = false;
    }

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="BootStrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="css/stylesheet.css">
        <title></title>
        <script type="text/javascript">

         function myFunction()
        {
            document.getElementById("myH2").style.color = "#ff0000";

        }

        </script>
    </head>
    <body >
        <div class="container jumbotron">

        <form action="" method="post">

            <div class="btn-group-xs">
                <label for="name" id="myH2">Name</label>
                <input type="text" name="name" >
            <button type="submit" id="btn1" name="save" <?php echo $isSaveDisabled?'disabled':''; ?>onclick="myFunction();">Save</button>
            <button type="submit" id="btn2" name="create" <?php echo $isCreateDisabled?'disabled':'';?>>Create</button>
            </div>


        </form>
        </div>


    </body>
</html>

1 个解决方案

#1


1  

You are using this example: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_style_color

您正在使用此示例:http://www.w3schools.com/jsref/tryit.asp?filename =tryjsref_style_color

If you just refer to the original code, you'll see that the button type is button, not submit.

如果你只是参考原始代码,你会看到按钮类型是按钮,而不是提交。

So this will work:

这样可行:

<button type="button" id="btn1" ...and the rest as in your code

#1


1  

You are using this example: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_style_color

您正在使用此示例:http://www.w3schools.com/jsref/tryit.asp?filename =tryjsref_style_color

If you just refer to the original code, you'll see that the button type is button, not submit.

如果你只是参考原始代码,你会看到按钮类型是按钮,而不是提交。

So this will work:

这样可行:

<button type="button" id="btn1" ...and the rest as in your code