如何在php中传递javascript函数

时间:2019-02-09 08:34:26

标签: javascript php

1我编写了这段代码,并且在没有php条件的情况下也可以正常工作。

<select sorted class='form-control required' onchange="saveDetailData(<?php echo $detail->id ?>,$(this).val(),'batchNo' )">
 <option value='0'>Select Batch</option>

 <?php if(!empty($batchDetail) && $userInfo[0]->isManual == 1)
  {
  foreach ($batchDetail as $cl19)
      {
      ?>
 <option value='<?php echo $cl19->batchNo ?>' <?php if ($cl19->batchNo == $detail->batchNo) { echo 'selected=selected';} ?>><?php echo date('d-M-Y', strtotime(date('Y-m-d', strtotime($cl19->inwardDate)))).  ' - ' . $cl19->batchNo .' ( '.$cl19->stockQty.' )'?></option>
         <?php
       }
   }
 ?>                                              
</select>

2并且此代码在PHP条件下不起作用。

<?php
  if($userInfo[0]->isManual == 1){echo "
    <select sorted class='form-control required' onchange='saveDetailData($detail->id,$(this).val(),'batchNo' )'>
   <option value='0'>Select Batch</option>
 ";}
?>

  <?php if(!empty($batchDetail) && $userInfo[0]->isManual == 1)
    {
   foreach ($batchDetail as $cl19)
      {
  ?>
   <option value='$cl19->batchNo' <?php if ($cl19->batchNo == $detail->batchNo) { echo 'selected=selected';} ?>><?php echo date('d-M-Y', strtotime(date('Y-m-d', strtotime($cl19->inwardDate)))).  ' - ' . $cl19->batchNo .' ( '.$cl19->stockQty.' )'?></option>
      <?php
       }
    }
?>
<?php
 if($userInfo[0]->isManual == 1){echo "
  </select>
    ";}
 ?>

3这是我无法正常工作的代码的输出:

<select sorted="" class="form-control required" onchange="saveDetailData(244,$(this).val()," batchno'="" )'="">

    <option value="0">Select Batch</option>                                                                                                               
    <option value="t51">25-Oct-2018 - t51 ( 5 )</option>                                                                                                                       
    <option value="t58">25-Dec-2018 - t58 ( 410 )</option>

</select>

4这是工作代码的输出:

<select sorted="" class="form-control required" onchange="saveDetailData(244,$(this).val(),'batchNo' )">

    <option value="0">Select Batch</option>                                                                                                               
    <option value="t51">25-Oct-2018 - t51 ( 5 )</option>                                                                                                                       
    <option value="t58">25-Dec-2018 - t58 ( 410 )</option>

唯一的问题是,当我将php条件放在2号上时,它没有进入函数onchange,检查3号上的输出。

我要做的就是将此代码置于if($userInfo[0]->isManual == 1)的php条件中

我用这个解决了:

if($userInfo[0]->isManual == 1){echo '
<select sorted class="form-control required" onchange="saveDetailData('.$detail->id.',$(this).val(),\'batchNo\' )">
   <option value="0">Select Batch</option>
     ';}
?>

1 个答案:

答案 0 :(得分:0)

尝试

    <select sorted class='form-control required' <?php if ($userInfo[0]->isManual == 1) { ?> onchange="saveDetailData('<?php echo $detail->id ?>', $(this).val(), 'batchNo')" <?php } ?>>