我想在 javascript 中检查一个 php 变量并相应地创建一个数组。
我试过这个,但它不工作:
<script language="Javascript" type="text/javascript">
var phpvar1_large = <?php echo $var1_large ?>;
var phpvar2_large = <?php echo $var2_large ?>;
var phpvar3_large = <?php echo $var3_large ?>;
var phpvar4_large = <?php echo $var4_large ?>;
if(!phpvar1_large){
var imgArray = new Array(
'<?=$main_img; ?>'
);
}else if(!phpvar2_large){
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>'
);
}else if(!phpvar3_large){
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>',
'<?=$var2_large; ?>',
'<?=$var3_large; ?>'
);
}else if(!phpvar4_large){
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>',
'<?=$var2_large; ?>',
'<?=$var3_large; ?>'
);
}else if(phpvar4_large){
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>',
'<?=$var2_large; ?>',
'<?=$var3_large; ?>',
'<?=$var4_large; ?>'
);
}
</script>
但是,如果我不使用 if 语句,则会正确创建数组:
<script language="Javascript" type="text/javascript">
var imgArray = new Array(
'<?=$main_img; ?>',
'<?=$var1_large; ?>',
'<?=$var2_large; ?>',
'<?=$var3_large; ?>',
'<?=$var4_large; ?>'
);
</script>
非常感谢您的帮助!