如何在使用php javascript小部件单击submit时运行jquery
我使用链接中的PHP/Javascript-alexmarandon小部件 在我的项目中:如何在使用php javascript小部件单击submit时运行jquery,php,jquery,widget,Php,Jquery,Widget,我使用链接中的PHP/Javascript-alexmarandon小部件 在我的项目中: script.js html.php demo.html 在脚本i中,使用以下代码: (function() { // Localize jQuery variable var jQuery; /******** Load jQuery if not present *********/ if (window.jQuery === undefined || window.jQuery.fn.jque
script.js
html.php
demo.html
在脚本i中,使用以下代码:
(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.7.1') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main();
}
/******** Our main function ********/
function main() {
jQuery(document).ready(function($) {
/******* Load CSS *******/
var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "style.css"
});
css_link.appendTo('head');
/******* Load HTML *******/
var jsonp_url = "data.php?callback=?";
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html(data);
});
$('.submit').click(function(){
alert('test');
});
});
}
})(); // We call our anonymous function immediately
(函数(){
//本地化jQuery变量
var-jQuery;
/********如果不存在jQuery,则加载它*********/
if(window.jQuery==undefined | | window.jQuery.fn.jQuery!=='1.7.1'){
var script_tag=document.createElement('script');
script_tag.setAttribute(“type”、“text/javascript”);
script_tag.setAttribute(“src”,
"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
if(script_tag.readyState){
script_tag.onreadystatechange=function(){//用于旧版本的IE
如果(this.readyState=='complete'| | this.readyState=='loaded'){
scriptLoadHandler();
}
};
}否则{
script_tag.onload=scriptLoadHandler;
}
//尝试查找头部,否则默认为documentElement
(document.getElementsByTagName(“head”)[0]| | document.documentElement).appendChild(script_标记);
}否则{
//窗口上的jQuery版本就是我们想要使用的版本
jQuery=window.jQuery;
main();
}
/********加载jQuery后调用******/
函数scriptLoadHandler(){
//将$和window.jQuery恢复为其以前的值并存储
//本地jQuery变量中的新jQuery
jQuery=window.jQuery.noConflict(true);
//调用我们的主要函数
main();
}
/********我们的主要职能********/
函数main(){
jQuery(文档).ready(函数($){
/*******加载CSS*******/
var css_link=$(“”,{
rel:“样式表”,
键入:“文本/css”,
href:“style.css”
});
css_link.appendTo('head');
/*******加载HTML*******/
var jsonp_url=“data.php?callback=?”;
$.getJSON(jsonp_url,函数(数据){
$('#示例小部件容器').html(数据);
});
$('.submit')。单击(函数(){
警报(“测试”);
});
});
}
})(); // 我们立即调用匿名函数
然后我调用data.php中的数据
$str = '<div class="widget_advance">';
$str .= '<form method="post" id="form_widget_advance">';
$str .= '<input type="submit" value="Tìm" class="submit" />';
$str .= '</form>';
$str .= '</div>';
echo $_GET['callback'] . '(' . json_encode($str) . ');';
$str='';
$str.='';
$str.='';
$str.='';
$str.='';
echo$\u GET['callback'].'(“.json_encode($str)。”);
最后,在demo.html中是结果:
<script src="script.js" type="text/javascript"></script>
<div id="example-widget-container"></div>
=>当我点击按钮提交时,结果不显示
警报('test')
;如何安装它?将其放在内部:
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html(data);
$('.submit').click(function(){
alert('test');
});
});
你看,它必须在回调里面。如果提交按钮不在DOM中,则无法绑定到该按钮。您还可以执行以下操作:$(“#someForm”).submit(function(){…})代码>