我有一个jQuery getJSON,我想将一些数据传递给php。奇怪的是我无法传递变量。在我的代码中,我还有另一个工作正常的领域,如果我对字符串进行硬编码也可以正常工作,但是如果我放置一个有效的变量,该变量也可以正常工作,则不起作用。这是我的代码:
这是我从下拉列表中获取字符串的地方:
$('.funcionarias').change(function(){
func = $('.funcionarias').val();
$('.funcionariaTitle').text(func);
});
这是我使用相同的getjson并且工作正常的另一部分:
$.getJSON(url+preco+php, {serv:idserv},null).then(function(data)
{console.log(data);
但这不起作用:
$.getJSON("http://ib.esy.es/gestao/_php/select_com_mes.php", {func:func}).then(function(data)
如果我把:
$.getJSON("http://ib.esy.es/gestao/_php/select_com_mes.php", {func:"String"}).then(function(data)
工作正常。
所以,我真的不知道发生了什么。该变量即使在 getJSON 下方也能正常工作,但在该特定 getJSON 方法下不起作用。有人可以帮助我吗?谢谢!
<script>
var func = "";
$('.funcionarias').change(function(){
func = $('.funcionarias').val();
$('.funcionariaTitle').text(func);
});
var showServAdicionais = 1;
var url = "";
var php = ".php"
var preco = "preco";
$(".adi1").click(function(){
$(".serv2").show();
showServAdicionais = 2;
});
$(".add2").click(function(){
$(".serv3").show();
showServAdicionais = 3;
});
$ (".add3").click(function(){
$(".serv4").show();
showServAdicionais = 4;
});
$(".add4").click(function(){
$(".serv5").show();
showServAdicionais = 5;
});
$(function(){
var items="";
$.getJSON("http://ib.esy.es/gestao/_php/servicos_categorias.php",function(data){
$.each(data,function(index,item)
{
items+="<option value='"+item.ID+"'>"+item.categoria+"</option>";
});
$("#categoria").html(items);
});
});
var url = "";
function selectCat(){
$('#servicos').change(function() {
$('.serv'+showServAdicionais).val($(this).find(":selected").text());
});
//for textbox use $('#txtEntry2').val($(this).find(":selected").text());
var e = document.getElementById("categoria");
var servSelected = e.options[e.selectedIndex].value;
var items="";
if(servSelected === "1"){
url = "http://ib.esy.es/gestao/_php/servicos_threading";
}
if(servSelected === "2"){
url = "http://ib.esy.es/gestao/_php/servicos_sobrancelhas";
}
if(servSelected === "3"){
url = "http://ib.esy.es/gestao/_php/servicos_manicure";
}
$.getJSON(url+php,function(data){
$.each(data,function(index,item)
{
items+="<option value='"+item.ID+"'>"+item.servico+"</option>";
});
$("#servicos").html(items);
});
};
function selectServ(){
var idserv = $("#servicos option:selected").val();
$.getJSON(url+preco+php, {serv:idserv},null).then(function(data)
{console.log(data);
$(".valor"+showServAdicionais).val(data.preco);
$(".total"+showServAdicionais).val(data.preco);
});
}
$('.toCalculate').keyup( getDiff);
function getDiff(){
var num1=1*$('.valor'+showServAdicionais).val() || 0;
var num2=1*($('.desconto'+showServAdicionais).val())/100 || 0;
var num = (Math.abs(num1 * num2 -num1))
$('.total'+showServAdicionais).val(num.toFixed(2))
}
function showCom(){
var pass = document.getElementById('comissoes').value;
var namefuncionaria = $('.funcionariaTitle').text();
if(func == "Iara" && pass == "teste"){
$('.showComissoes').show()
} else if(func == "Katiuska" && pass == "teste1"){
$('.showComissoes').show()
} else if(func == "Lorena" && pass == "teste2"){
$('.showComissoes').show()
} else if(func == "Adryely" && pass == "teste3"){
$('.showComissoes').show()
} else{
alert("Senha errada");
}
}
function cleanInput(){
$('#comissoes').val("");
}
function hideCom(){
$('.showComissoes').hide();
}
$.getJSON("http://ib.esy.es/gestao/_php/select_com_mes.php", {func:func}).then(function(data)
{console.log(data);
var tr = data
for (var i = 0; i < data.report.length; i++) {
var tr = $('<tr/>');
// Indexing into data.report for each td element
$(tr).append("<td>" + data.report[i].Mes + "</td>");
$(tr).append("<td>" + data.report[i].Total + "</td>");
$('.tableMes').append(tr);
}
});
function teste(){
alert(func)
}
</script>
更新:
好吧,我发现getJSON响应归因于div请求的"Lorena",
<div class="funcionariaTitle">Lorena</div>
是我的回应,即使我在这里改变
var func = "";
$('.funcionarias').change(function(){
func = $('.funcionarias').val();
$('.funcionariaTitle').text(func);
});
其文本。如果我请求下拉值,例如:
var funcionaria = $( ".funcionarias option:selected" ).text();
它根本没有给出任何回应