[英]Can't get right syntax for JavaScript variables inside php print
[英]echoing javascript inside of php, can't get quotes right
我想讓一些基於php if語句的javascript執行,但是我無法正確獲取鏈接的引號。在控制台中,它顯示出以下行缺少a)。 我確定我搞砸了引號,當我剛使用javascript時,它是正確的,但是現在與echo混合了,我聽不到了。 我在整個事情上使用單引號,並在里面加倍。 其余的都在工作,請看看並幫助我解決此問題。 謝謝!
$(this).append("<a href=""index.php?patient=test&a="".""appointment_nums[count]""."">Schedule Appointment </a>");
if ($mode== 'view' && $action==''){
echo '<script >
$(document).ready(function(e){
$("body").click(function(event) {
redirect = $(event.target).context.getAttribute("href");
});
checkColumns();
});
function checkColumns(){
count=0;
appointment_nums = [];
$(".mgrid_table > tbody > tr").each(function() {
appointment_nums.push($(this).find("td").eq(3).find("label").html());
appointment_nums = appointment_nums.filter(function(n){ return n != undefined });
});
appointments = appointment_nums.length;
appendColumns();
}
function appendColumns(){
function ajax() {
return $.ajax({
type:"post",
url: "../testrequest.php",
data : {appointment_nums:appointment_nums},
dataType:"json",
});
};
ajax().done(function(result){
$("table:nth-of-type(2) > tbody > tr > td:nth-of-type(2)").each(function() {
if($(this).children().length < 1){
if (result[count] == false){
$(this).append("<a href=""index.php?patient=test&a="".""appointment_nums[count]""."">Schedule Appointment </a>");
}else{
$(this).append("<span>Waiting For Doctor to Schedule</span>");
}
}
count = count + 1 ;
});
});
}
</script>';
要么更改報價類型,要么開始轉義。 例如,使用此原始(和損壞的)代碼段:
.append("<a href="foo.php" onclick="someFunction("hi mom!");">");
您實際上有3級代碼。 有原始的Javascript(第1層):
.append("<a href="foo.php" onclick="someFunction("hi mom!");">");
^--------------layer 1--------------------------------^
然后是嵌入式HTML(第2層):
.append("<a href="foo.php" onclick="someFunction("hi mom!");">");
^-------^ ^----------layer 2---------^
然后是子嵌入的Javascript(第3層):
.append("<a href="foo.php" onclick="someFunction("hi mom!");">");
^-------^
在每種情況下,都必須使用適合嵌入其中的圖層的引號。 因此,要將第2層引號嵌入第1層,請使用轉義:
.append("<a href=\"foo.php\" onclick=\"someFunction(\"hi mom!\");\">");
因此,現在您在第1層有了有效的Javascript。但是HTML本身仍然壞了,因為一旦將該代碼段嵌入到DOM中,您最終就會
<a href="foo.php" onclick="someFunction("hi mom!");">
^---start attribute
^---end attribute
^--start unterminated attribute
因此,現在您必須擔心將第3層嵌入第2層。因為它是Javascript-in-html:
.append("<a href=\"foo.php\" onclick=\"someFunction("hi mom!");">\");
^--------^----------^ etc... layer 2-in-1 escapes
^^^^^--------^^^^^ layer 3-in-1 quotes
基本上-您始終必須考慮將在其中看到代碼的上下文。 如果要在javascript中嵌入HTML,則HTML中的所有引號也必須是有效的Javascript。 如果要在HTML中嵌入Javascsript,則任何JS引號也必須產生有效的HTML。 而且,當您將語言嵌套3或4級深時,必須考慮所有層。 每一層本身必須對所有父層均有效。
使用單引號.. IE
$(this).append('<a href="index.php?patient=test&a=' . 'appointment_nums[count]' . '>Schedule Appointment </a>');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.