繁体   English   中英

在php中回显javascript,无法正确获取报价

[英]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(&quot;hi mom!&quot;);">\");
                 ^--------^----------^ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM