Javascript进入PHP foreach循环

时间:2016-02-10 15:58:06

标签: javascript php foreach

我正在尝试设置图像日历按钮,以便将日期输入到由foreach循环生成的文本框中。我使用了这段代码,但它只生成了第一个盒子的日历(循环的第一次迭代)。有什么方法可以让它适用于所有迭代?

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<script>
  $(function() {
    $( "#datepicker" ).datepicker({
      showOn: "button",
      buttonImage: "../bulkmail/images.png",
      buttonImageOnly: true,
      buttonText: "Select date"
    });
  });
</script>


echo '<table>';
foreach( $records as $key => $record){

     $Counter++;
    //start of row
    echo '<tr>'; 
    $ListName = $record->getField($fields[1]);
    $Priority = $record->getField($fields[2]);
    $TargetDate = $record->getField($fields[3]);
    $MailDate = $record->getField($fields[5]);

    $ListName_td = '<td class="LN">'.$ListName.'</td>';
    $Priority_td = '<td class="P">'.$Priority.'</td>';
    $TargetDate_td = '<td class="TD">'.$TargetDate.'</td>';
    $MailDate_td = '<td class="MD"><input type="text" id="datepicker" " name="MailDate'.$Counter.'"  value="'.$MailDate.'"/>
                                   </td>';

    echo '<tr>';}
    echo '<table>';

2 个答案:

答案 0 :(得分:0)

您使用了ID(#datepicker),但ID应该在页面中是唯一的。在您的输入中使用类:class="datepicker",在JS中使用$(".datepicker")作为CSS选择器。

答案 1 :(得分:0)

像这样使用:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<script>
  $(function() {
    $( ".datepicker" ).datepicker({
      showOn: "button",
      buttonImage: "../bulkmail/images.png",
      buttonImageOnly: true,
      buttonText: "Select date"
    });
  });
</script>


echo '<table>';
foreach( $records as $key => $record){

     $Counter++;
    //start of row
    echo '<tr>'; 
    $ListName = $record->getField($fields[1]);
    $Priority = $record->getField($fields[2]);
    $TargetDate = $record->getField($fields[3]);
    $MailDate = $record->getField($fields[5]);

    $ListName_td = '<td class="LN">'.$ListName.'</td>';
    $Priority_td = '<td class="P">'.$Priority.'</td>';
    $TargetDate_td = '<td class="TD">'.$TargetDate.'</td>';
    $MailDate_td = '<td class="MD"><input type="text" class="datepicker" " name="MailDate'.$Counter.'"  value="'.$MailDate.'"/>
                                   </td>';

    echo '<tr>';}
    echo '<table>';