Javascript into PHP foreach loop

Javascript into PHP foreach loop

本文关键字:loop foreach PHP into Javascript      更新时间:2023-09-26

我正在尝试设置一个图像日历按钮以将日期输入到由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>';

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

像这样使用它:

<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>';