javascript – 根据数据库中的ordernumber更改行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 根据数据库中的ordernumber更改行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1881字,纯文字阅读大概需要3分钟。
内容图文
![javascript – 根据数据库中的ordernumber更改行](/upload/InfoBanner/zyjiaocheng/912/d8d887321c114192b4c7fe842fc907ff.jpg)
我正在为我的教育项目创建一个披萨订购网站.在stackoverflow-community的帮助下,我已经取得了很多成就 – 谢谢!但现在我陷入困境,无法找到解决问题的方法.
题
如何根据数据库中的ordernumber(mysqli)交替更改行颜色(白色/灰色/白色/灰色…)? ordernumber可以在多行中,所以我不能简单地逐行更改颜色.
我已尝试使用jquery,但仅当订购号始终保留在列表中时才有效(偶数/奇数)…如果订单被取消,则它不再有效(请参阅缺少订单号7的图片)
这是jquery中的代码:
$(document).ready(function() {
var check = 0;
for(var i =0; i<= $("tr").length;i++){
$("tr").each(function(){
if(parseInt($(this).find("#bestnr").text())==check){
if(check%2 == 0){
$(this).css("background-color","white");
}else{
$(this).css("background-color","#DCDCDC");
}
}
});
check +=1;
}
});
有任何想法吗?谢谢你的帮助!
解决方法:
因为你正在使用JQuery,所以这样的事情应该做的 – 代码注释中的解释.
$(document).ready(function() {
// define the initial "previous order id" as 0 assuming
// there will never be an order with id 0
var previousOrderId = 0;
var previousBgColour = '#dcdcdc';
var thisBgColour;
// loop the table rows
$("tr").each(function() {
// determine "this" row id (assuming bestnr is short for bestelnummer)
// and that the text in that table cell *is* the order number
// I've changed this to a class as an id HAS to be unique
// you'll need to update your code to accommodate
var thisOrderId = parseInt($(this).find(".bestnr").text());
// define the background colour based on whether the order id has changed
// if it has change it
if(thisOrderId != previousOrderId) {
thisBgColour = previousBgColour == '#dcdcdc' ? '#ffffff' : '#dcdcdc';
previousBgColour = thisBgColour;
}
else {
thisBgColour = previousBgColour;
}
$(this).css({'background-color' : thisBgColour});
//update the previousOrderId to this id
previousOrderId = thisOrderId;
});
});
您基本上存储了以前的订单ID并将其与当前订单ID进行比较 – 如果订单ID未更改,则将使用之前的背景颜色,如果它已将其翻转为备用颜色.
内容总结
以上是互联网集市为您收集整理的javascript – 根据数据库中的ordernumber更改行全部内容,希望文章能够帮你解决javascript – 根据数据库中的ordernumber更改行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。