【问题标题】:Javascript into PHP foreach loopJavascript 进入 PHP foreach 循环
【发布时间】:2016-02-10 15:58:05
【问题描述】:

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

【问题讨论】:

  • 我只是想知道,getField() 方法的目的是什么?
  • $records的内容是什么?
  • 变量$fields在哪里定义?
  • 您缺少结束 &lt;/tr&gt; 标记。你有&lt;tr&gt;
  • 我会投票结束这个问题,语法错误太多。先解决这个问题,然后尝试给我们一些反馈。

标签: javascript php foreach


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    像这样使用它:

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

    【讨论】:

    • 使用class而不是id。
    猜你喜欢
    • 2014-04-19
    • 2014-03-09
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2012-07-19
    • 1970-01-01
    相关资源
    最近更新 更多