これをチェックしていただきありがとうございます:
私がしていることは、名前やクラスなどの複数の値を読み取ってクリックされているかどうかを判断し、サーバー上のPHPにパラメーターとして送信して、クラスと名前の値を切り替えることができるようにすることです。
これはうまく機能し、最初にクリックするとクラスが切り替わりますが、要素を2回目にクリックすると機能しなくなります。Chrome のコンソールを見ると、POST の結果が成功していることがわかりますが、$.post コールバック関数はもう実行されていません。
$('a').click(function() {
if($(this).attr("name")=="Config"){
return;
}
else if ($(this).attr("name")=="Return"){
return;
}
else if ($(this).attr("name")=="Temperatura"){
return;
}
else{
var parameters = {};
parameters.devicename = $(this).attr("name").split('_')[0];
parameters.devicevalue = $(this).attr("name").split('_')[1];
parameters.devicestate = parseInt($(this).html());
alert (parameters.devicestate);
var selector;
if ($(this).hasClass("Luz_On") || $(this).hasClass("Luz_Off")){
selector = "Luz";
$(this).attr('id','ajaxelement');
alert("entro");
alert($(this).attr('name'));
alert(selector);
alert($(this).attr('id'))
}
else if ($(this).hasClass("Cortina_Up") || $(this).hasClass("Cortina_Down")){
selector = "Cortina";
$(this).attr('id','ajaxelement');
}
else if ($(this).hasClass("Temperatura")){
selector = "Temperatura";
$(this).attr('id','ajaxelement');
}
else if ($(this).hasClass("Rewind")){
selector = "Rewind";
$(this).attr('id','ajaxelement');
}
else if ($(this).hasClass("Play")){
selector = "Play";
$(this).attr('id','ajaxelement');
}
else if ($(this).hasClass("Pause")){
selector = "Pause";
$(this).attr('id','ajaxelement');
}
else if ($(this).hasClass("Forward")){
selector = "Forward";
$(this).attr('id','ajaxelement');
}
$.post(
"http://localhost/AplicacionWeb/test.php",
parameters,
function(data){
if (data.valid=='true')
{
switch(selector)
{
case "Luz":
alert("case");
if ($('#ajaxelement').attr('name') == "Luz_On"){
alert("caseif1");
$('#ajaxelement').removeClass("Luz_On").addClass("Luz_Off");
$('#ajaxelement').attr('name','Luz_Off');
$('#ajaxelement').removeAttr('id');
}
if ($('#ajaxelement').attr('name') == "Luz_Off"){
alert("caseif2");
$('#ajaxelement').removeClass("Luz_Off").addClass("Luz_On");
$('#ajaxelement').attr('name','Luz_On');
$('#ajaxelement').removeAttr('id');
}
break;
case "Cortina":
break;
case "Temperatura":
break;
case "Rewind":
break;
case "Play":
break;
case "Pause":
break;
case "Forward":
break;
default:
alert("C'est la vie");
}
}//data.valid
},
"json"
);
}
}); // end click