[英]form not defined in Firefox
i'm trying to get the value of the element option: 我正在尝试获取element选项的值:
<div id="decorazioni_agg">Decorazioni aggiuntive:<br>
<select id="decorazioni" name="decorazioni">
<option class="option" value="smarties">Smarties</option>
<option class="option" value="scaglie">Scaglette di
cioccolato</option>
<option class="option" value="cheerios">Cheerios</option>
<option class="option" value="chocopops">Choco
Pops</option>
</select><br>
<input class="button" name="aggiungi" type="button" value="Aggiungi
decorazione" onClick="add_decoration(decorazioni.value)">
</div>
And this is the function add_decoration(obj): 这是函数add_decoration(obj):
function add_decoration(tipo)
{
if(tipo == 'smarties')
add_smarties();
else
add_something(tipo);
}
Now, this is working both with Chrome and IE8 but not with Firefox 3.5; 现在,这既适用于Chrome和IE8,又不适用于Firefox 3.5。 the firefox error console says:
火狐错误控制台说:
decorazioni is not defined
How can i do without using a bunch of switch so i can keep my code clean? 不使用大量开关怎么办才能保持代码干净? Thanks in advance
提前致谢
onclick="var elem = document.getElementById('decorazioni'); add_decoration(elem.options[elem.selectedIndex].value);"
However, it would be better to use proper event registration instead of inline events. 但是,最好使用适当的事件注册而不是内联事件。 Here's a jQuery example;
这是一个jQuery示例; to make it work give the button an id:
为了使它工作,请给按钮一个id:
$('#buttonid').on('click', function() {
add_decoration($('#decorazioni').val());
});
Do not bind event handlers in HTML. 不要在HTML中绑定事件处理程序。
<input class="button" id="aggiungi" name="aggiungi" type="button" value="Aggiungi decorazione" />
document.getElementById('aggiungi').onclick= function () {
add_decoration(document.getElementById('decorazioni').value);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.