5

質問を詳しく説明しましょう。私はPHPとsmartyを使用しています。op=backクエリ文字列から値を取得しています。しかし、これは PHP ファイルが実行されるたびに発生するわけではないためop=back、特定のリンクでクリック イベントを開始する必要があるのはいつですか。私のPHPファイルコードスニペットは以下のとおりです:

<?php  
  include_once("includes/teacher-application-header.php");

  prepare_request();
  $request = empty( $_GET ) ? $_POST : $_GET ;
  $op = $request['op'];

  $objTeachClassSub = new TeacherClassesSubjects();

  global $teacher_profile_from_session;

  $teacher_id = $teacher_profile_from_session['TEACHER_ID'];

  $teacher_classes_subjects = $objTeachClassSub->GetClassSubjectMappingsbyTeacherId($teacher_id);

$smarty->assign('teacher_classes_subjects', $teacher_classes_subjects); 

  $smarty->assign("op",$op);        
  $smarty->display("teacher-details.tpl");      
?>

smarty ファイルのコード スニペットは次のようになります。

{literal}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
</script>
{/literal}
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" >
    <tr>
        <td align="left" valign="top">
          <h3>Teacher Details</h3>
        </td>
    </tr>
</table>
<table width="99%" border="0" cellpadding="0" cellspacing="0" class="manage_box" >
    <tr>
      <td>
            <table cellpadding="0" cellspacing="0" border="1" width="100%">
        <tr>
            <td width="25%">
            {if $teacher_classes_subjects}
               {foreach from=$teacher_classes_subjects item="classes_subjects"}
                 <b><a href="#" id="{$classes_subjects.class_id}">{$classes_subjects.class_name}</a></b><br />
                 {if $classes_subjects.class_subjects}
                 <div id="class_subjects_{$classes_subjects.class_id}">
                   {foreach from=$classes_subjects.class_subjects item="class_subjects"}
                        <i><a id ="back" href="chapter_details.php?class_id={$classes_subjects.class_id}&cs_map_id={$class_subjects.cs_map_id}">{$class_subjects.subject_name}</a></i><br />
                   {/foreach}
                 </div>
                 <br />
                 {/if}
               {/foreach}
            {/if}
          </td>  
          <td width="75%">
          {if $chapter_details}
            <ul>
            {foreach from=$chapter_details item=chapter}
            <li><a href="chapter_details.php?op=get_chapter_theory&chapter_id={$chapter.chapter_id}&class_id={$class_id}&cs_map_id={$cs_map_id}&chapter_title={$chapter.chapter_title}">{$chapter.chapter_title}</a></li>
            {/foreach}
            </ul>
          {/if}

          {include file=$file_to_show}
          </td>  aly what is
        </tr>
      </table>
        </td>
    <td align="left" id="subject_container" valign="top">
    </td>
    <td align="left" id="chapter_container" valign="top">
    </td>
  </tr>
</table>

ここで達成したいのは、上記のスマート テンプレート ファイルに jQuery コードを記述し、op 値が back(つまり{op==back}) の場合に実行されるようにすることです。実際に予想されるのはop=back、次のリンクでクリック イベントが発生するタイミングです。

<a id ="back" href="chapter_details.php?class_id={$classes_subjects.class_id}&cs_map_id={$class_subjects.cs_map_id}">{$class_subjects.subject_name}</a>

この機能を実現する方法を誰かに説明してもらえますか? 前もって感謝します。

4

4 に答える 4

1

I guess your best option is to set it to a hidden html input in your template, and in your java script in the document ready checking the value of that html input, then changing the href attribute of your link

于 2013-05-31T19:58:43.357 に答える
1

op==back のときに特定のサイトに移動したい場合は、php コードでリダイレクトする必要があります

 if(isset($_GET['op']) && $_GET['op'] == 'back'){
      header('location: yourpath/youurl.php');
 }

何かをエコーする前に、これを行うことを忘れないでください。

このオプションが設定されているときにクリックをトリガーしたい場合は、jQuery を使用している場合は、準備が整ったドキュメントと $('#back').trigger('click') で条件付きでスクリプトを印刷するだけです。

 //$go = (isset($_GET['op']) && $_GET['op'] == 'back');
 {if $go}
 <script>
   $(document).ready(function(){
        $('#back').trigger('click');
   });
 </script>
 {/if}

編集

そのリンクをクリックすると、e.preventDefault() などを使用していない限り、ブラウザは別のページに移動します。まだお持ちでない場合は、私が提案した最初のソリューションを使用することをお勧めします。何らかの理由でクライアントにアクセスするためのリクエストが必要な場合、リダイレクトする場合、または何か他のことを行う場合は、2 番目のソリューションを使用します。

それが役に立てば幸い

于 2013-05-31T12:26:24.560 に答える
0

純粋な JS で関数を作成して、パラメーターのクエリ文字列値を取得し、それを使用してリンク クリックを起動するかどうかを決定できます。

このようなもの:

function getQueryStringParameter(name) {

    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

var n = getQueryStringParameter('op');
if(n == 'back'){
    $('#back').trigger('click');
}

それが役に立てば幸い。

于 2013-06-04T03:36:43.013 に答える