我试图将函数的参数值存储到变量。
var store;
function load_event(page_id){
store = page_id;
alert(page_id); //Works fine
}
alert(store); //Undefined???
page_id是从另一个PHP文件获取的参数(以便PHP文件正在调用该函数)。
我不明白为什么此存储变量返回未定义?
PHP代码!
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
$post = $wp_query -> post;
$post_id = $post->ID;
echo $post_id; //Get the post_id of the current post.
?>
<script>
loadEvent("<?php echo json_encode($post_id); ?>");
</script>
</div><!-- #content -->
</div><!-- #primary -->
如果您load_event()
之前致电alert(store)
。原因是您正在使用alert(store)
该函数之前load_event()
。此时,in中的值store
是不确定的。这是因为该load_event()
函数直到您调用它后才alert(store)
执行,先执行该函数,然后求值store
到undefined
,然后load_event()
执行,将其值设置store
为page_id
。移动load_event()
之前alert(store)
将解决问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句