php - 如何根据通过查询字符串接收到的值执行 jQuery 代码?

标签 php jquery smarty

让我详细解释一下这个问题。我正在使用 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 值返回(即 {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>

谁能解释一下如何实现这个功能?提前致谢。

最佳答案

如果你想在 op==back 时转到一个特定的站点,你应该在你的 php 代码中重定向它

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

请记住在回显任何内容之前执行此操作。

如果您想在设置此选项时触发点击,只需有条件地打印一个脚本,并准备好文档和 $('#back').trigger('click') 如果您使用的是 jQuery:

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

编辑

单击该链接将使浏览器转到另一个页面,除非您使用了类似 e.preventDefault() 的方法。如果您还没有,我建议您使用我提出的第一个解决方案。 如果您需要请求到达客户端,然后出于任何原因进行重定向,或者您执行其他操作,我会使用第二种解决方案。

希望对你有帮助

关于php - 如何根据通过查询字符串接收到的值执行 jQuery 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16856940/

相关文章:

javascript - 如何在 TinyMCE 中设置响应式文件管理器返回绝对 url

html - 纯 PHP/HTML View VS 模板引擎 View

php - CakePHP - 执行 hasMany 的正确方法

没有排序值的 PHP fetch_array/sql

php - 使 pdo::query 静态

php - Smarty,每 4 次迭代新行

javascript - 来自 SMARTY 和 Controller 初始化的 AngularJS 集合

php - 具有一个或多个(多个)参数的搜索表单

javascript - url jquery Accordion 中没有主题标签

javascript - jQuery UI - 可排序未触发?