如何在jquery中将带数学运算的字符串声明为变量


how to declare string with math operation as variable in jquery

PHP:

<?php
   $test = "100-12";
?>

jQuery:

var test = <?php echo $test ?>;
alert(test);

在开发工具源代码(F12)中,它看起来像这样:

var test = 100-12;

但当已经提醒/通过其他页面中的数据或刚刚提醒时,测试的值变为89,这是当100减去12时的值。

我需要100-12的原样值,而不是减法。如何阻止jQuery减去字符串。

我尝试了String()到String(),甚至尝试了"+,但都不起作用。

如果你想保留它(没有算术运算),用引号括起来。

因此,如果:

<?php
$test = "101-12"; // if this is the origin value
?>
<script type="text/javascript">
var test = '<?php echo $test; ?>'; // just put quotes in here as well, so that it does not interpolate it as intergers and do arithmentic
alert(test);
</script>

var test = "101-12";将完成您的工作