我有以下代码:
var led = <?php echo get_field('why_led'); ?>;
var optics = <?php echo get_field('why_led'); ?>;
var ledil = <?php echo get_field('why_led'); ?>;
function toLED() {
document.getElementById("change-box").innerHTML = "<p class='small'>" + led + "</p>";
document.getElementById("p-button").style.backgroundColor = "yellow";
document.getElementById("p-button").style.color = "black";
document.getElementById("p-button2").style.backgroundColor = "black";
document.getElementById("p-button2").style.color = "white";
document.getElementById("p-button3").style.backgroundColor = "black";
document.getElementById("p-button3").style.color = "white";
}
第二个和第三个文本框还有另外两种方法。 问题是,我似乎没有找到真正回应文本的方法。 我只是介于PHP未定义的函数和语法错误之间。
我想从ACF字段“why_led”获取文本,然后将其回显到JS框并将其保存到变量中。
答案 0 :(得分:3)
选项一:将vars置于func
中 function toLED() {
var led = "<?php echo get_field('why_led'); ?>";
var optics = "<?php echo get_field('why_led'); ?>";
var ledil = "<?php echo get_field('why_led'); ?>";
document.getElementById("change-box").innerHTML = "<p class='small'>" + led + "</p>";
document.getElementById("p-button").style.backgroundColor = "yellow";
document.getElementById("p-button").style.color = "black";
document.getElementById("p-button2").style.backgroundColor = "black";
document.getElementById("p-button2").style.color = "white";
document.getElementById("p-button3").style.backgroundColor = "black";
document.getElementById("p-button3").style.color = "white";
}
选项二:将它们分配给窗口
window.led = "<?php echo get_field('why_led'); ?>";
window.optics = "<?php echo get_field('why_led'); ?>";
window.ledil = "<?php echo get_field('why_led'); ?>";
function toLED() {
document.getElementById("change-box").innerHTML = "<p class='small'>" + window.led + "</p>";
document.getElementById("p-button").style.backgroundColor = "yellow";
document.getElementById("p-button").style.color = "black";
document.getElementById("p-button2").style.backgroundColor = "black";
document.getElementById("p-button2").style.color = "white";
document.getElementById("p-button3").style.backgroundColor = "black";
document.getElementById("p-button3").style.color = "white";
}
答案 1 :(得分:1)
在为JS变量分配php值时,您没有应用引号“”。 应用引号并在函数内部写入变量,它将起作用。
function toLED() {
var led = "<?php echo get_field('why_led'); ?>";
var optics = "<?php echo get_field('why_led'); ?>";
var ledil = "<?php echo get_field('why_led'); ?>";
document.getElementById("change-box").innerHTML = "<p class='small'>" + led + "</p>";
document.getElementById("p-button").style.backgroundColor = "yellow";
document.getElementById("p-button").style.color = "black";
document.getElementById("p-button2").style.backgroundColor = "black";
document.getElementById("p-button2").style.color = "white";
document.getElementById("p-button3").style.backgroundColor = "black";
document.getElementById("p-button3").style.color = "white";
}
答案 2 :(得分:0)
从你的问题,
这取决于您的输入数据&#34; why_led&#34;这里。
如果是字符串,那么您当前的代码将无法正常工作。
您应将其更改为:
var led = "<?php echo get_field('why_led'); ?>";
alert(led);
顺便说一句,您可以在grome或firefox中使用控制台来查看错误