简体   繁体   English

我想更改按钮的颜色和文本onclick,刷新页面时不应更改

[英]i want to change button color and text onclick and it should not change when page is refreshed

i want to change button colour and text on on click and it should not change when page is refreshed.Here when set status is clicked it changes the text to confirmed and colour changes to green,when i again click on it,it changes the colour to red and text changes to pending.Now when i again click on it,it should change the colour to green and text should change to confirmed,here neither the color changes nor the text is changed.And this should stay when page is refreshed. 我要更改按钮的颜色和单击时的文本,刷新页面时不应更改。在此处单击设置状态时,它将文本更改为确认,颜色更改为绿色,当我再次单击时,它更改颜色更改为红色,文本更改为待处理。现在,当我再次单击它时,它应该将颜色更改为绿色,并且文本应该更改为已确认,此处颜色既不变也不更改文本。刷新页面后,该属性应该保持不变。 here is the code: 这是代码:

<input type="submit" id="<?php echo $orderQuery['id'];?>" value="set status" onclick="setColor('<?php echo $orderQuery['id'];?>')"; />
<script>

var count = 1;

function setColor(btn) {
var property = document.getElementById(btn);

if (count == 0) {property.style.backgroundColor = "#ff0000"
 if (property.value=="confirmed") property.value = "pending";
 }
 else {property.style.backgroundColor = "#7FFF00"
 count = 0;
if (property.value=="set status") property.value = "confirmed";
}

    }
</script>

HTTP is Stateless Protocol we cannot maintain the state between HTTP Calls for more info look at this answer HTTP是无状态协议,我们无法维持HTTP调用之间的状态以获取更多信息,请查看此答案

So you have to save the data that you need next time (In your case button color) by other mechanism like Query Strings,Cookies or Sessions 因此,您必须通过其他机制(例如查询字符串,Cookies或会话 )保存下一次需要的数据(以您的情况为按钮颜色)

QUERY STRINGS 查询字串

you can use query string to carry the information that you want to preserve like 您可以使用查询字符串来携带您想要保留的信息,例如

www.example.com?btnColor=blue

and ofcourse at the page loading you can set the color with php something like this. 当然,在页面加载时,您可以使用php设置颜色,如下所示。

//pseudo code
if(isset($_GET['btnColor']) // for query string
 echo  '<button style="background:'.$_GET['btnColor'].'">'
if(isset($_COOKIE['btnColor']) // for cookie
 echo  '<button style="background:'.$_COOKIE['btnColor'].'">'
if(isset($_SESSION['btnColor']) // for Session based storage
 echo  '<button style="background:'.$_SESSION['btnColor'].'">'

COOKIES & SESSIONS 饼干和会议

you can also use the cookies to save your btn color 您还可以使用Cookie来保存btn颜色

setcookie("color", "red", time() + (86400 * 30), "/"); // 86400 = 1 day
// and you can retrieve the cookie value from


 echo $_COOKIE['color'] // prints red

The sessions can be used in following way 可以通过以下方式使用会话

//to set the session
$_SESSION['color'] = "red";
// to retrive the data
echo $_SESSION['color'];

The major advantage of cookie and sessions are they save the information over a period For more information and detailed information please look at php.net documentation. Cookie和会话的主要优点是它们可以在一段时间内保存信息。有关更多信息和详细信息,请参阅php.net文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM