将javascript块带入php?

时间:2014-07-30 11:10:44

标签: javascript php checkbox

我想用这个:

<script type="text/javascript">
    (function() {
        var checked = false;
        $('button.check-all').click(function() {
            checked = !checked;
            $('.checkbox-class').prop('checked', checked); // jQuery 1.6+
            if (checked) $(this).text('Uncheck All');
            else $(this).text('Check All');
        });
    })();
</script>

iside php code,

echo <<< CHECKALL

 <script src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript">
    (function() {
        var checked = false;
        $('button.check-all').click(function() {
        console.log("checked");
            checked = !checked;
            $('.permissionsCheckBox').prop('checked', checked); // jQuery 1.6+
            if (checked){ $(this).text('Uncheck All');}
            console.log("looopChecked");
            else { $(this).text('Check All');}
        });
    })();
</script>
CHECKALL;

但我在else { $(this).text('Check All');}收到语法错误 我不确定$(this)符号适用于php javascript ??

2 个答案:

答案 0 :(得分:4)

因为你有这个

console.log("looopChecked");
在if条件之后

。之后你有其他条件不是有效条件。否则必须在if条件之后立即放置。所以你必须改变这个

if (checked){
   $(this).text('Uncheck All');
}
console.log("looopChecked");
else {
   $(this).text('Check All');
}

到这个

if (checked){
   $(this).text('Uncheck All');
}
else {
   $(this).text('Check All');
}

并且在条件检查之前或者在if条件内或者在else条件内部执行此操作时,将继续登录控制台。

答案 1 :(得分:4)

If和Else语句之间有console.log("looopChecked")

if (checked){ $(this).text('Uncheck All');}
console.log("looopChecked");
else { $(this).text('Check All');}

必须

if (checked){ $(this).text('Uncheck All');}
else { $(this).text('Check All');}

提示:始终缩进代码。它有助于你避免这种语法错误。