【问题标题】:Error not run code jquery when echo jsoncallback回显jsoncallback时出错不运行代码jquery
【发布时间】:2012-02-22 15:29:07
【问题描述】:

在文件 index.php 中我使用代码:

<?php
$str = "<script src='click.js'></script>"
$str .= '<div class="box_content_reset"><input type="submit" value="reset" class="reset" /></div>';
echo $_GET["jsoncallback"] . '(' . json_encode($str) . ');';
?>

在文件 click.js 中我使用代码:

<script>
        jQuery(document).ready(function($) { 
            $(".reset").click(function() {
                alert("Jquery running ...");
            });
        });
    }
})(); 
</script>

=> 结果没有运行 jquery,如何解决?

【问题讨论】:

  • 查看控制台日志,有没有错误? HTML 代码的其余部分是什么?

标签: jquery jsonp


【解决方案1】:

你正在使用:

<input type="submit" value="reset" class="reset" />

这意味着当您单击按钮时,您的页面将自动刷新。如果您想使用点击事件,请使用:

<input type="button" value="reset" class="reset" />

用你的 js 文件

<script>
    jQuery.noConflict();
    jQuery(document).ready(function($) { 
        jQuery(".reset").click(function() {
            alert("Jquery running ...");
        });
    });
}
})(); 
</script>

jQuery.noConflict(); 这样您的代码就不会与mootools 等发生冲突。

【讨论】:

  • 它不会刷新在警报出现之前(并且出现的警报会清楚地显示脚本正在运行)
猜你喜欢
  • 2011-12-29
  • 2014-04-26
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多