0

我使用 php 来创建一个字符串,该字符串使用通过使用 post 的表单在上一页上提交的数据。

这是php:

if ($carrier== "DL") {   


                $front = '<tr><td><div onclick="selectText(this)">EZE*#$*IET';
                $center =  '/O$'.$FOP.'/RB'.$differencePos.'X0.00P'.$fee.'AT0.00/OIECP'.$orIssue.'111062789#MCRV*/MA'.$MCOamt; 
                $end = '</div></td></tr>';


                               echo $front,$guest1Ticket,'X*A',$guest1Con,$center,'#N1.1',$end;

             if($guest2Ticket) echo $front,$guest2Ticket,'X*A',$guest2Con,$center,'#N2.1',$end;  

             if($guest3Ticket) echo $front,$guest3Ticket,'X*A',$guest3Con,$center,'#N3.1',$end;  

             if($guest4Ticket) echo $front,$guest4Ticket,'X*A',$guest4Con,$center,'#N4.1',$end;  

             if($guest5Ticket) echo $front,$guest5Ticket,'X*A',$guest5Con,$center,'#N5.1',$end;  

             if($guest6Ticket) echo $front,$guest6Ticket,'X*A',$guest6Con,$center,'#N6.1',$end;  

             if($guest7Ticket) echo $front,$guest7Ticket,'X*A',$guest7Con,$center,'#N7.1',$end;  

             if($guest8Ticket) echo $front,$guest8Ticket,'X*A',$guest8Con,$center,'#N8.1',$end;  

             if($guest9Ticket) echo $front,$guest9Ticket,'X*A',$guest9Con,$center,'#N9.1',$end; 
        } //if ($airline == "DL")

这将创建如下所示的字符串: EZE*#$*IET8388623729002X*A/O$CA/RB71.02X0.00P50.00AT0.00/OIECP01AUG111062789#MCRV*/MA21.02#N1.1

EZE*#$*IET8388623729003X*A/O$CA/RB71.02X0.00P50.00AT0.00/OIECP01AUG111062789#MCRV*/MA21.02#N2.1

EZE*#$*IET8388623729004X*A/O$CA/RB71.02X0.00P50.00AT0.00/OIECP01AUG111062789#MCRV*/MA21.02#N3.1

....

每行之间的唯一区别是 IET 8388623729004部分和弦本身末端的 N 1.1部分。

我使用$front$end子字符串将实际字符串包装在 div 和 table 元素中以用于布局目的。

给每个得到回显的 div 一个唯一的 id 以便我的onclick="selectText(this)"工作的最佳方法是什么?

我知道我可以$front分成几个子字符串,然后做类似的事情

if($guest2Ticket) echo $front1,'id="div2"',$front2,$guest2Ticket,'X*A',$guest2Con,$center,'#N2.1',$end;

但是有更好的方法吗?

4

2 回答 2

1

这就是我会做的方式。通过$front从变量字符串更改为函数,您可以使用静态计数器并在每次调用函数时增加它

function front() {
    static $i=1;
    return '<tr><td><div id="div'.$i.'" onclick="selectText(this)">EZE*#$*IET';
    $i++;
}

它看起来像这样

if ($carrier== "DL") {   


            function front() {
                  static $i=1;
                  return '<tr><td><div id="div'.$i.'" onclick="selectText(this)">EZE*#$*IET';
                  $i++;
             }
            $center =  '/O$'.$FOP.'/RB'.$differencePos.'X0.00P'.$fee.'AT0.00/OIECP'.$orIssue.'111062789#MCRV*/MA'.$MCOamt; 
            $end = '</div></td></tr>';


                           echo front(),$guest1Ticket,'X*A',$guest1Con,$center,'#N1.1',$end;

         if($guest2Ticket) echo front(),$guest2Ticket,'X*A',$guest2Con,$center,'#N2.1',$end;  

         if($guest3Ticket) echo front(),$guest3Ticket,'X*A',$guest3Con,$center,'#N3.1',$end;  

         if($guest4Ticket) echo front(),$guest4Ticket,'X*A',$guest4Con,$center,'#N4.1',$end;  

         if($guest5Ticket) echo front(),$guest5Ticket,'X*A',$guest5Con,$center,'#N5.1',$end;  

         if($guest6Ticket) echo front(),$guest6Ticket,'X*A',$guest6Con,$center,'#N6.1',$end;  

         if($guest7Ticket) echo front(),$guest7Ticket,'X*A',$guest7Con,$center,'#N7.1',$end;  

         if($guest8Ticket) echo front(),$guest8Ticket,'X*A',$guest8Con,$center,'#N8.1',$end;  

         if($guest9Ticket) echo front(),$guest9Ticket,'X*A',$guest9Con,$center,'#N9.1',$end; 
} //if ($airline == "DL")
于 2013-03-16T13:37:33.607 回答
1

我会用 jquery 来做这件事,根本不发出 id。

(不要忘记首先包含 jquery.js)。

<script>
  $(function()
  {
    $('div').click(function()
    {
      selectText(this);
    });
  });
</script>
于 2013-03-16T13:54:57.470 回答