0

在我的表的 STATUS 列中可能有 3 个值(已发布、取消、尚未发布)

每个值都有其指定的颜色。我使用以下代码做到了这一点:

if($posted == 'posted')
                {
                $posted = "<b style='color:blue'>posted</b>";

                }
                elseif($posted == 'cancel')
                {
                $posted = "<b style='color:red'>cancel</b>";
                }
                else
                {
                $posted = "<b style='color:green'>not yet posted</b>";

                }

但我需要通过 javascript 传递值。具有样式代码时无法传递值。<b style='color:green'>

我尝试使用mysql_real_escape_string,但它失去了颜色,所有内容都传递给父窗口,包括代码 style='color:green'> 我有什么方法可以转义特殊字符。?不失色彩风格。?请帮忙

这些值来自我的数据库。它存储在这里;

$db_array[] = array("serial_no" => "$serialno", "model" => "$model", "desc" => "$description", "series" => "$dr_srs", "refno" => "$dr_no", "custname"=> "$name", "client_code"=> "$client_code", "ctelno" => "$phone", "contpers" => "$name2", "brand" => "$brandname", "unit_type" => "$unit_type", "posted" => "$posted", "address" => "$address", "city" => "$city"); 

数组显示在表格中:

foreach($db_array as $key => $value)
            {
                if(($key % 2) == 0)
                {

                ?>
                        <tr class='hoverstate2' onclick="clicked('<?php echo $value['serial_no']; ?>','<?php echo $value['posted']; ?>','<?php echo $value['model']; ?>','<?php echo $value['desc']; ?>','<?php echo $value['series']; ?>','<?php echo $value['refno']; ?>','<?php echo $value['custname']; ?>','<?php echo $value['client_code']; ?>','<?php echo $value['ctelno']; ?>','<?php echo $value['contpers']; ?>','<?php echo $value['brand']; ?>','<?php echo $value['unit_type']; ?>','<?php echo $value['posted']; ?>','<?php echo $value['address']; ?>','<?php echo $value['city']; ?>')">

                        <td ><?php echo $value['serial_no']; ?></td>
                        <td ><?php echo $value['model']; ?> </td>
                        <td ><?php echo $value['desc']; ?></td>
                        <td ><?php echo $value['posted']; ?></td></tr>
                <?php
                }

当点击它是通过这个javascript:

    function clicked(serial_no,posted,model,desc,series,refno,custname,client_code,ctelno,contpers,brand,unit_type,posted,address,city)

{
    var client_code = client_code;
    var serialno = serial_no;
    var posted = posted;
    var model = model;
    var desc = desc;
    var series = series;
    var refno = refno;
    var custname = custname;
    var ctelno = ctelno;
    var contpers = contpers;
    var brand = brand;
    var posted = posted;
    var address = address;
    var city = city;

}
4

1 回答 1

1

有很多方法可以解决您的情况。像这样的东西:

在你的 CSS 中:

.posted{
  color:blue;
}

.cancel{
 color:red;
}

.default{
 color:green;
}

在您的 PHP 中,您可以尝试使用 switch 语句,而不是使用 if-else 语句。你可以像这样实现它:

switch($posted){
  case 'posted':
    $posted = "<b class='posted'>posted</b>";
    break;
  case 'cancel':
   $posted = "<b class='cancel'>cancel</b>";
    break;
  default:
   $posted = "<b class='default'>not yet posted</b>";
   break;
}

看我改变了你的风格并把它放到了一个类中。更干净,在你的 javascript 中你只需要调用你想要的类。

于 2013-07-29T03:51:00.997 回答