所以,我有以下 php :
<script language="javascript" type="text/javascript">
var counter = 1;
var split;
var rightAnswer;
function potvrdi(){
counter = getCookie("counter");
if(counter == "")counter = 1;
var potvrdi = document.getElementById("gmedu").innerHTML;
if(potvrdi == "Potvrdi"){
checkAnswer();
document.getElementById("gmedu").innerHTML = "Dalje";
}
else{
alert(counter);
setCookie("counter",(parseInt(counter)+1));
doSplit();
document.getElementById("gmedu").innerHTML = "Potvrdi";
document.getElementById("answer1text").style.color = "black";
document.getElementById("answer2text").style.color = "black";
document.getElementById("answer3text").style.color = "black";
document.getElementById("answer4text").style.color = "black";
}
return false;
}
function setCookie(cname, cvalue) {
document.cookie = cname + "=" + cvalue + "; ";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
function doSplit(){
var text = "1.\r\n"+...\\here is a huge string of text
split = text.split(/\b\d+\./);
var split1 = split[counter].split(/\n\w+\)/);
rightAnswer = split1[1];
split1 = randomizeAnswers(split1);
document.getElementById("question").innerHTML = split1[0];
document.getElementById("answer1text").innerHTML = split1[1];
document.getElementById("answer2text").innerHTML = split1[2];
if(split1.length > 3){
document.getElementById("answer3text").innerHTML = split1[3];
document.getElementById("a3").style.visibility = "visible";
}
else
document.getElementById("a3").style.visibility = "hidden";
if(split1.length > 4){
document.getElementById("answer4text").innerHTML = split1[4];
document.getElementById("a4").style.visibility = "visible";
}
else
document.getElementById("a4").style.visibility = "hidden";
}
function randomizeAnswers(split1){
var length = split1.length-1;
var random = (parseInt(Math.random()*length))+1;
var tmp = split1[1];
split1[1] = split1[random];
split1[random] = tmp;
return split1;
}
function checkAnswer(){
var checkedButton = getCheckedRadio(document.getElementsByName("answer"));
var id = checkedButton.id;
if(id=="answer1"){
var answer1 = document.getElementById("answer1text");
console.log(answer1.innerHTML)
console.log(rightAnswer)
if(rightAnswer === document.getElementById("answer1text").value){
answer1.style.color = "#33FF00";
alert("TACNO!");
}
else{
answer1.style.color = "red";
alert("NETACNO!");
}
}
else if(id=="answer2"){
var answer2 = document.getElementById("answer2text");
if(rightAnswer == (answer2.innerHTML))
answer2.style.color = "#33FF00";
else
answer2.style.color = "red";
}
else if(id=="answer3"){
var answer3 = document.getElementById("answer3text");
if(rightAnswer == (answer3.innerHTML))
answer3.style.color = "#33FF00";
else
answer3.style.color = "red";
}
else if(id=="answer4"){
var answer4 = document.getElementById("answer4text");
if(rightAnswer == (answer4.innerHTML))
answer4.style.color = "#33FF00";
else
answer4.style.color = "red";
}
}
function getCheckedRadio(radio_group) {
for (var i = 0; i < radio_group.length; i++) {
var button = radio_group[i];
if (button.checked) {
return button;
}
}
return undefined;
}
</script>
然后 js :
add_action( 'wp_ajax_some_ajax', 'some_ajax' );
add_action( 'wp_ajax_nopriv_some_ajax', 'some_ajax' );
function some_ajax()
{
if (some_condition)
{
/*Some function*/
}
}
所以,我想为调试目的添加某种警报。如您所见,它是一个ajax函数。我尝试添加jQuery警报但没有成功(或者不确定警报应该出现在哪里)。
有人可以告诉我如何设置警报以进行调试吗?
由于
答案 0 :(得分:1)
在你的HTML中
<!DOCTYPE html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php some_ajax() ?>
</body>
</html>
和你的php代码
function some_ajax()
{
echo "<script type='text/javascript'>alert('Something happened');</script>";
}