在if条件下难以使用$(document).data() [英] Having difficulty using $(document).data() within an if condition
问题描述
我有以下代码,可以将value
成功存储为name
,然后可以将其用作php中的全局变量.这是完整的代码(除了标签& jquery链接):
<script>
$(document).data('name', "value");
</script>
<?php $Phpvar = "<script>document.writeln($(document).data('name'));</script>";
print_r($Phpvar); ?> //Prints "value"
但是,如果我将$(document).data
函数放在if条件内,则此操作将停止工作:
<head>
<script>
if ($("div").hasClass("blah")){
$(document).data('name', "value");
}
</script>
</head>
<body>
<div class = "blah"></div>
<?php $Phpvar = "<script>document.writeln($(document).data('name'));</script>";
print_r($Phpvar); ?> //Prints "undefined"
</body>
我尝试了$(document).ready
,但这仍然无法正常工作. (another variable
绝对等于0):
<head>
<script>
$(document).ready(function(){
if ($("div").hasClass("blah")){
$(document).data('name', "value");
}
});
</script>
</head>
<body>
<div class = "blah"></div>
<?php $Phpvar = "<script>document.writeln($(document).data('name'));</script>";
print_r($Phpvar); ?> //Prints "undefined"
<body>
在保持在PHP中使用document.writeln($(document).data('name'));
的功能的同时,如何在$(document).data('name', "value");
中添加条件?
显而易见的结论是leveldeterminant实际上并不等于0.在if语句之前添加console.log(leveldeterminant)并检查其值>
I have the following code, which successfully stores value
as name
which I can then use as a global variable within php. This is the full code (aside from tags & jquery link):
<script>
$(document).data('name', "value");
</script>
<?php $Phpvar = "<script>document.writeln($(document).data('name'));</script>";
print_r($Phpvar); ?> //Prints "value"
However, this stops working if I put the $(document).data
function within an if condition:
<head>
<script>
if ($("div").hasClass("blah")){
$(document).data('name', "value");
}
</script>
</head>
<body>
<div class = "blah"></div>
<?php $Phpvar = "<script>document.writeln($(document).data('name'));</script>";
print_r($Phpvar); ?> //Prints "undefined"
</body>
I tried $(document).ready
but that still doesn't work. (another variable
is definitely equal to 0):
<head>
<script>
$(document).ready(function(){
if ($("div").hasClass("blah")){
$(document).data('name', "value");
}
});
</script>
</head>
<body>
<div class = "blah"></div>
<?php $Phpvar = "<script>document.writeln($(document).data('name'));</script>";
print_r($Phpvar); ?> //Prints "undefined"
<body>
How can I add a condition to my $(document).data('name', "value");
while maintaining the capability to use document.writeln($(document).data('name'));
in my php?
The obvious conclusion is that leveldeterminant does not in fact equal 0. Add a console.log(leveldeterminant) before your if statement and check what value it holds
这篇关于在if条件下难以使用$(document).data()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!