在我的SQL中,echo $ row ['ans']的类型为varchar(11):JAN,FEB,MAR,APR。我现在知道为什么它不会被显示出来,但是当我将JAN,FEB,MAR,APR改为数字时它会起作用。请告诉你我的知识和理解。谢谢!
选择框
var months = [<?php echo '"'.implode('","',explode(",",$rows['ans'])).'"'; ?> ];
months.sort( function() { return Math.random() - .5 } );
for ( var i=0; i<4; i++ ) {
$('<div>' + months[i] + '</div>').data( 'months', i ).appendTo( '#monthPile' ).draggable( {
//containment: '#content',
stack: '#monthPile div',
cursor: 'move',
revert: true
} );
}
创建广告位
var slot = [<?php echo '"'.implode('","',explode(",",$rows['ans'])).'"'; ?> ];
for ( var i=0; i<=3; i++ ) {
$('<div>' + slot[i] + '</div>').data( 'months', i ).appendTo( '#monthSlots' ).droppable( {
accept: '#monthPile div',
hoverClass: 'hovered',
drop: handleCardDrop
} );
}
}
答案 0 :(得分:0)
将字符串注入javascript时,您需要在这些字符周围加上“”来告诉它是一个字符串。您的号码有效,因为没有“”javascript将这些字符解释为数字。尝试使用php implode函数在数组中的字符串周围添加双引号(假设$ row ['ans']包含数组)
更新工作示例:
<?php
$rows = array(
"ans" => "JAN,FEB,MAR"
);
?>
<html>
<body>
<script type="text/javascript">
var test = [<?php echo '"'. implode('","',explode(",",$rows['ans'])) . '"'; ?>];
document.write(test.toString());
</script>
</body>
</html>