简体   繁体   English

速记,否则将php与javascript混合无法正常工作

[英]shorthand if else mixing php with javascript not working

Im attempting to add a checked attribute in an input field in javascript that will be outputted as html 我正在尝试在将输入为html的javascript的输入字段中添加checked属性

Here is my line.. 这是我的电话..

var checked = ( <?php echo $original_delivery_date; ?> == json['dates_per_zone'][i]) ? "checked" : "";

I have verified that both json['dates_per_zone'][i] and $original_delivery_date are correct and displaying, but nothing is appearing. 我已经验证了json['dates_per_zone'][i]$original_delivery_date都正确并且可以显示,但是什么也没出现。

Is that the correct syntax? 那是正确的语法吗?

My final line is then... 我的最后一行是...

html += '<input ' + checked + ' name="delivery_date" value="' + json['dates_per_zone'][i] + '">

It should be: 它应该是:

var checked = ( '<?php echo $original_delivery_date; ?>' == json['dates_per_zone'][i] ? "checked" : "" );

You're missing the quotes around the string that PHP prints. 您缺少PHP打印的字符串周围的引号。

使用日期作为字符串:像这样:

var checked = ('<?php echo $original_delivery_date; ?>' == json['dates_per_zone'][i] ? "checked" : "" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM