通过Javascript将html更改为html保留一个变量
在我的第一个webapp上工作,与此有冲突,我希望有一个简单的解决方案:/ 我有这个profile.html,它只有html结构,我用php和我的数据库动态填充它,到目前为止都很酷 我还有一个search-results.html,它通过一堆查询“Post”开始根据过滤器生成结果。。。到目前为止一切都很好 现在的问题是,我需要能够向我的profile.html发送正确的id,当用户从search-results.html点击一个产品时,我给每个产品一个数据产品id,问题是我卡住了,因为我只是不知道如何在产品点击时打开profile.html,然后让profile.html知道实际点击了哪个产品。。。这里有一些代码,只是为了更容易理解我在这里的情况 profile.html通过Javascript将html更改为html保留一个变量,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,在我的第一个webapp上工作,与此有冲突,我希望有一个简单的解决方案:/ 我有这个profile.html,它只有html结构,我用php和我的数据库动态填充它,到目前为止都很酷 我还有一个search-results.html,它通过一堆查询“Post”开始根据过滤器生成结果。。。到目前为止一切都很好 现在的问题是,我需要能够向我的profile.html发送正确的id,当用户从search-results.html点击一个产品时,我给每个产品一个数据产品id,问题是我卡住了,因为我只是不知
$(document).ready(function(){
var num=(Math.random()*3+1);
num=num|0;
num=num.toString();
var n=num.toString();
ajaxPost("Path/get.php", { id : n}, function(result){
var json=JSON.parse(result);
if(json.error==0){
console.log(json);
$('img#fotoProfile').attr('src',json.response.profile_picture);
var nombreFoodtruckConCiudadyPais = json.response.name + " <span class='subtitle'>@" + json.response.cities + "</span>";
$('h2.profile-name').html(nombreFoodtruckConCiudadyPais);
$('#foodtruckName').html(json.response.name);
$('div#descripcion p#descripcionText').html(json.response.description);
$('a#emailText').attr('href',json.response.mail);
$('div#email a#emailText').html(json.response.mail);
$('div#rating p#ratingText').html(json.response.rating);
$('div#categoria p#categoriaText').html(json.response.category);
var origen = json.response.cities + ', ' + json.response.states + ', ' + json.response.countries;
$('div#origen p#origenText').html(origen);
$('div#telefono p#telefonoText').html(json.response.phone);
}else{
alert("Foodtruck Not Found");
}
});
基本上我就被困在那里了。我的x应该是我的个人资料上的我的n.html ajaxPost id:n
我希望我能得到一些帮助,我是网络应用的新手。。。提前感谢。您需要将“x”作为参数添加到profile.html URL,即profile.html&idft=x
$(document).on('click', '[data-idFT]', function(){
var x=$(this).data('idFT');
var url = "profile.html?idft="+x;
window.location.href = url;
})
如果使用JavaScript检索profile.html上的参数,请使用本文中的一些技术
或者听起来您可能需要在PHP中使用它,如果您访问数据库,那么您只需要:
<?php
echo htmlspecialchars($_GET["idft"]);
?>
如果您喜欢Ajax呈现数据而不刷新页面,我会选择更简单的方法:
$(document).on('click', '[data-idFT]', function(){
var x=$(this).data('idFT');
myfunction(x);
});
现在将其传递给Ajax:
function myfunction(n)
{
ajaxPost("Path/get.php", { id : n}, function(result){
var json=JSON.parse(result);
if(json.error==0){
console.log(json);
$('img#fotoProfile').attr('src',json.response.profile_picture);
var nombreFoodtruckConCiudadyPais = json.response.name + " <span class='subtitle'>@" + json.response.cities + "</span>";
$('h2.profile-name').html(nombreFoodtruckConCiudadyPais);
$('#foodtruckName').html(json.response.name);
$('div#descripcion p#descripcionText').html(json.response.description);
$('a#emailText').attr('href',json.response.mail);
$('div#email a#emailText').html(json.response.mail);
$('div#rating p#ratingText').html(json.response.rating);
$('div#categoria p#categoriaText').html(json.response.category);
var origen = json.response.cities + ', ' + json.response.states + ', ' + json.response.countries;
$('div#origen p#origenText').html(origen);
$('div#telefono p#telefonoText').html(json.response.phone);
}else{
alert("Foodtruck Not Found");
}
});
}
函数myfunction(n)
{
ajaxPost(“Path/get.php”,{id:n},函数(结果){
var json=json.parse(结果);
if(json.error==0){
log(json);
$('img#fotoProfile').attr('src',json.response.profile_picture);
var nombreFoodtruckConCiudadyPais=json.response.name+“@”+json.response.cities+”;
$('h2.profile name').html(nombreFoodtruckConCiudadyPais);
$('#foodtruckName').html(json.response.name);
$('div#descripionp#descripiontext').html(json.response.description);
$('a#emailText').attr('href',json.response.mail);
$('div#email a#emailText').html(json.response.mail);
$('div#rating p#ratingText').html(json.response.rating);
$('div#categoria p#categoriaText').html(json.response.category);
var origen=json.response.cities+'、'+json.response.states+'、'+json.response.countries;
$('div#origen p#origenText').html(origen);
$('div#telefono p#telefonoText').html(json.response.phone);
}否则{
警报(“未找到食物卡车”);
}
});
}
2个小的附带问题,我如何将该url发送到浏览器,以及如何在个人资料中捕获它,提前感谢我的更新答案,回答您的附带问题吗?“window.location.href'是更改URL的内容,$\u GET[]检索参数查询字符串应该以profile.html?idft=yes这样的问题开头,但我在search-results.html中没有能够构建myfunction、、的html,,,您是否建议删除实际代码,并将其替换为profile.html body中的同一函数?@AlbertoCantu您可以添加html代码吗?然后我尝试找到一个实用的解决方案
function myfunction(n)
{
ajaxPost("Path/get.php", { id : n}, function(result){
var json=JSON.parse(result);
if(json.error==0){
console.log(json);
$('img#fotoProfile').attr('src',json.response.profile_picture);
var nombreFoodtruckConCiudadyPais = json.response.name + " <span class='subtitle'>@" + json.response.cities + "</span>";
$('h2.profile-name').html(nombreFoodtruckConCiudadyPais);
$('#foodtruckName').html(json.response.name);
$('div#descripcion p#descripcionText').html(json.response.description);
$('a#emailText').attr('href',json.response.mail);
$('div#email a#emailText').html(json.response.mail);
$('div#rating p#ratingText').html(json.response.rating);
$('div#categoria p#categoriaText').html(json.response.category);
var origen = json.response.cities + ', ' + json.response.states + ', ' + json.response.countries;
$('div#origen p#origenText').html(origen);
$('div#telefono p#telefonoText').html(json.response.phone);
}else{
alert("Foodtruck Not Found");
}
});
}