首页 > 解决方案 > 将字段值设置为 javascript 变量

问题描述

我有一个名为样式的 html 字段,它从 php 值中获取其值:

<input name="styles" value="'.$colorstyle.'" id="styles" >

现在我有一个 javascript 代码,我需要在其中放置字段的值:

this.colours = {
        'normal' : {
            'txt' : "rgb(255,255,255)",
            'arrow':"rgb(0,0,0)",

        }
    };

我在 javascript 文件中尝试了以下代码,但没有得到正确的输出。另外,我不知道如何调试。

var arrowcolor = document.getElementById('styles').innerHTML
    this.colours = {
        'normal' : {
            'txt' : "rgb(255,255,255)",
            'arrow':"'+arrowcolor+'",
        }
    };

标签: javascriptphphtml

解决方案


使用 value 而不是 innerHTML

var arrowcolor = document.getElementById('styles').value
    this.colours = {
        'normal' : {
            'txt' : "rgb(255,255,255)",
            'arrow':arrowcolor,
        }
    };

推荐阅读