基于<select>选择id时出现意外的令牌==

时间:2017-12-19 01:43:03

标签: javascript

我在这里有一些简单的骰子滚动代码: var dselection = document.getElementById(dieselection);             var dice = {                 if(dselection ==&#39; d6&#39;){                   双方:6,                   roll:function(){                     var randomNumber = Math.floor(Math.random()* this.sides)+ 1;                     return randomNumber;                   }                 }                 if(dselection ==&#39; d8&#39;){                   双方:8,                   roll:function(){                     var randomNumber = Math.floor(Math.random()* this.sides)+ 1;                     return randomNumber;                   }                 }                 if(dselection ==&#39; d4&#39;){                   双方:4,                   roll:function(){                     var randomNumber = Math.floor(Math.random()* this.sides)+ 1;                     return randomNumber;                   }                 }                 if(dselection ==&#39; d10&#39;){                   双方:10,                   roll:function(){                     var randomNumber = Math.floor(Math.random()* this.sides)+ 1;                     return randomNumber;                   }                 }                 if(dselection ==&#39; d12&#39;){                   双方:12,                   roll:function(){                     var randomNumber = Math.floor(Math.random()* this.sides)+ 1;                     return randomNumber;                   }                 }                 if(dselection ==&#39; d100&#39;){                   双方:100,                   roll:function(){                     var randomNumber = Math.floor(Math.random()* this.sides)+ 1;                     return randomNumber;                   }                 }             }             //将骰子滚动到页面             function printNumber(number){               var placeholder = document.getElementById(&#39;占位符&#39;);               placeholder.innerHTML = number;             }             var button = document.getElementById(&#39; button&#39;);             button.onclick = function(){               var result = dice.roll();               printNumber(结果);             }; 当我尝试检查dselection等于if(dselection ==&#39;某些&#39;)时,我收到一个错误:diceroller:80 Uncaught SyntaxError:Unexpected token ==。 为什么我无法检查这里的选择是什么?不应该是document.getElementById()获取选择的值吗? 如果它成功的原因我不能简单地将它与所选的字符串进行比较? 骰子需要根据选择的内容进行滚动。 这是我的选择: &lt; select id =&#34; dieselection&#34;&gt;             &lt; option value =&#34; d4&#34;&gt; D4&lt; / option&gt;             &lt; option value =&#34; d6&#34;&gt; D6&lt; / option&gt;             &lt; option value =&#34; d8&#34;&gt; D8&lt; / option&gt;             &lt; option value =&#34; d10&#34;&gt; D10&lt; / option&gt;             &lt; option value =&#34; d12&#34;&gt; D12&lt; / option&gt;             &lt;选项值=&#34; d20&#34;&gt; D20&lt; / option&gt;             &lt; option value =&#34; d100&#34;&gt; D100&lt; / option&gt;         &LT; /选择&GT;

1 个答案:

答案 0 :(得分:4)

不需要所有这些if语句。难道您没有看到sides属性的值只是dselection的数字部分的模式吗?所以就这样做:

var dice = {
    sides: parseInt(dselection.substr(1), 10),
    roll: function () {
        var randomNumber = Math.floor(Math.random() * this.sides) + 1;
        return randomNumber;
    }
};

您也没有将dselection设置为输入值。它应该是:

var dselection = document.getElementById('dieselection').value;