如何在提交按钮上的javascript中更改标签颜色 [英] how to change label color in javascript on submit button
问题描述
朋友你好,我的代码有问题, 在这段代码中,我想按保存按钮时,"Hello PHP"的打印和名字的标签颜色会发生变化,但这是行不通的,当我将提交"输入按钮类型时,它会显示"Hello PHP",并且标签颜色不会发生变化,当我按钮的按钮类型,然后更改标签颜色,但不打印"hello PHP",我想同时单击保存按钮单击,请帮助我从此代码中删除
Hello Friend i have problem in my code, In this code i want when i press save button then 'Hello PHP' print and label color of FIRST NAME change but it's not work, when I put submit in button type then it's shows 'Hello PHP' and label color not change and when I button in button type then label color change but not print 'hello PHP', I want both on save button Click , Please Help me to Stuck out from this code
这是我的代码:
<?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>
推荐答案
标签在提交后立即变色,但页面立即刷新. 您可以通过检查是否单击了保存"按钮来为标签着色:
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>
这篇关于如何在提交按钮上的javascript中更改标签颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!