2016-03-28 11 views
1

私はopencartのビューファイルで作業しています。私は日付と時刻をそれぞれ設定する2つのテキストボックスを持っています。私はチェックボックスを持っています。その値はチェックされているとき1、チェックされていないとき0です。上記のチェックボックスがオンの場合は、日付と時刻のフィールドを非表示にする必要があります(完了まで)。彼らは今(現在まで)完了です。 現在のコード:HTMLでJS関数を呼び出す方法

HTML

<tr id="row_ship_date"> 
    <td> 
     Shipment Date: 
    </td> 
    <td> 
     <input type="text" id="ship_date" name="ship_date" class="date" onchange="getServices();" /> 
    </td> 
</tr> 
<tr id="row_ship_time"> 
    <td> 
     Shipment Time: 
    </td> 
    <td> 
     <input type="text" id="ship_time" name="ship_time" class="time" /> 
    </td> 
</tr> 

<?php 
    if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check{?> 
     <script type="text/javascript"> 
      document.getElementById('row_ship_date').style.display='none'; 
      document.getElementById('row_ship_time').style.display='none'; 
      getServices(); 
     </script> 
    <?php } 
?> 

JS:

function getServices()  { 
    alert('test'); 
    var ship_date = $('#ship_date').val(); 
    var ship_time = $('#ship_time').val(); 
    // code so on.. 
} 

現在の問題

1)チェックボックスのチェックがtrueの場合、私はgetServices()を呼び出すことができません(のPHPチェックifステートメント)そのfalseの場合は、dateのonchangeイベントを呼び出しますうまく動作します。私はあなたの構文を推測

function getServices()  { 
    alert('test'); 
    var ship_date = new Date(); 
    var ship_time = currentTime(); 
    // code so on.. 
} 
+0

PHPの条件が真である場合にブラウザコンソールをチェックしてください.JSエラーがあるかもしれません。 –

+0

PHPの条件が真を返す場合は、コードに問題はないようです(コメントステートメント//チェックボックスの値のチェックは中括弧 "{") –

+0

@ techie_28をコメントしていますが、なぜ私の関数が得られないのかいわゆる(JS関数)。 –

答えて

-1

:どのようにこの機能がif声明(両方のフィールドが隠されている)の下で呼び出された場合、私は、両方のテキストフィールドに、それぞれのようなものを現在の日付と現在の時刻を設定することができ

2)問題があります。 PHPスクリプト内でJavascriptを呼び出すことはできません。ここではこの部分:

<?php if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check{?> 
<script type="text/javascript"> 
document.getElementById('row_ship_date').style.display='none'; 
document.getElementById('row_ship_time').style.display='none'; 
getServices(); 
// <---- notice also here you have no </script> tag :) 
<?php }?> 

は、私はいくつかのマイナーな構文の問題を指摘しています:)

+0

私の悪い私は実際に私はこの終了タグが私のqueを編集している。 –

+0

よりもどうすればいいですか?PHPとJavascript間の通信を許可するには、AJAXを使用できる必要があります。それがPHPとJAVASCRIPTとの間の通信を許可する唯一の方法です。 –

0

あなたはそれを宣言する前に関数を呼び出すしようとしているため、問題があることがあります。 コードを内部にラップしようとします。

<script> 
// self executing function 
(function() { 
    // put your code here 

})(); 
</script> 

<?php 
      if (isset($current_date_shipment) && $current_date_shipment == 1) // check box value check 
    {?> 
      <script type="text/javascript"> 
        (function() { 
      document.getElementById('row_ship_date').style.display='none'; 
      document.getElementById('row_ship_time').style.display='none'; 
      getServices(); 
     })(); 
     </script> 
0

これを試してみてください。

<?php 
      if (isset($current_date_shipment) && $current_date_shipment == 1) { 
       $now = new DateTime(); 
       $date = $now->format('Y-m-d'); 
       $time = $now->format('H:i'); 
?> 
       <script type="text/javascript"> 
        $('#row_ship_date,#row_ship_time').hide(); 
        $('#ship_date').val(<?php echo $date; ?>);// this should set date value,Use appropriate PHP function. 
        $('#ship_time').val(<?php echo $time; ?>); // this should set time value,Use appropriate PHP function. 
        getServices(); 
       </script> 
      <?php } ?> 

あなたが任意の値を設定するために隠しフィールドを必要といけないが、jQueryの.val()を使用して、上記のように設定することができます。

「新しい日付/時刻値の文字列」を対応する値を含むPHP変数に置き換えるか、適切なPHP関数を使用して取得します。

PHP条件が実行される前に、getServicesという関数定義がjQuery &に存在することも確認してください。

+0

私は、urのコードを試して、コンソールでエラーが発生しました "Uncaught SyntaxError:missing)引数の後の日付の変数のために"このコードが使用されます "<?php $ now = new DateTime(); $ date = $ now-> format( ' –

+0

実行前に自分のコメント//チェックボックスの値チェックを削除しましたか?答えに私の編集内容をチェックしてください。 –

関連する問題