Jquery 如何制作js函数文件,以及如何通过ajax调用该文件来实现该功能
我试图通过ajax调用一个文件,其中包含javascript函数。我能够获取文件的内容(当我提醒它时,它会显示该文件中写入的所有内容),但我不知道如何调用文件中的函数 我的代码是: 要调用ajax运行该函数,请执行以下操作:Jquery 如何制作js函数文件,以及如何通过ajax调用该文件来实现该功能,jquery,ajax,Jquery,Ajax,我试图通过ajax调用一个文件,其中包含javascript函数。我能够获取文件的内容(当我提醒它时,它会显示该文件中写入的所有内容),但我不知道如何调用文件中的函数 我的代码是: 要调用ajax运行该函数,请执行以下操作: if($("#essentialbtn").click(function() { $.ajax("essentialplan.php").done(function(data) { alert(data); }); })); 文件:(名为es
if($("#essentialbtn").click(function() {
$.ajax("essentialplan.php").done(function(data) {
alert(data);
});
}));
文件:(名为essentialplan.php)
localStorage.setItem('essentialPlan',$('#essential').text();
setItem('essentialPrice',$('#priceA').text();
window.location.href=”http://incrediblewebpages-com.stackstaging.com/convertion%20bubble/conversion2.php";
将其更改为脚本文件并使用。确保同时移除
标记
同时删除if()
essentialplan.js
localStorage.setItem('essentialPlan', $('#essential').text());
localStorage.setItem('essentialPrice', $('#priceA').text());
window.location.href = "http://incrediblewebpages-com.stackstaging.com/convertion%20bubble/conversion2.php";
单击“代码”:
$("#essentialbtn").click(function() {
$.getScript("essentialplan.js");
})
将其更改为脚本文件并使用。确保同时移除
标记
同时删除if()
essentialplan.js
localStorage.setItem('essentialPlan', $('#essential').text());
localStorage.setItem('essentialPrice', $('#priceA').text());
window.location.href = "http://incrediblewebpages-com.stackstaging.com/convertion%20bubble/conversion2.php";
单击“代码”:
$("#essentialbtn").click(function() {
$.getScript("essentialplan.js");
})
首先,需要从
essentialplan.php
文件中删除脚本标记,以便内容/响应数据如下:
localStorage.setItem('essentialPlan', $('#essential').text());
localStorage.setItem('essentialPrice', $('#priceA').text());
window.location.href = "http://incrediblewebpages-com.stackstaging.com/convertion%20bubble/conversion2.php";
然后,您可以使用eval()
:
或者您可以使用$.getScript()
:
$("#essentialbtn").click(function() {
$.getScript("essentialplan.php").done(function(script, textStatus) {
// do whatever you want to do
});
});
附言:您不需要if()来设置JQuery单击事件
P.S(2):对
$.getScript
函数的回调是可选的首先,您需要从
essentialplan.php
文件中删除脚本标记,因此内容/响应数据应如下所示:
localStorage.setItem('essentialPlan', $('#essential').text());
localStorage.setItem('essentialPrice', $('#priceA').text());
window.location.href = "http://incrediblewebpages-com.stackstaging.com/convertion%20bubble/conversion2.php";
然后,您可以使用eval()
:
或者可以使用$.getScript()
:
$("#essentialbtn").click(function() {
$.getScript("essentialplan.php").done(function(script, textStatus) {
// do whatever you want to do
});
});
附言:您不需要if()来设置JQuery单击事件
第2页:$.getScript
函数的回调是可选的