[英]Javascript Forms Select Selectedindex
Having problems with the following - Not working as expected, getting JS doc errors as well. 遇到以下问题-无法正常工作,并且也出现JS文档错误。
try this fiddle: http://jsfiddle.net/maniator/egjF4/6/ 试试这个小提琴: http : //jsfiddle.net/maniator/egjF4/6/
and change the if line to: 并将if行更改为:
if (document.forms['myform'].selectbox1.selectedIndex == 2)
you need the ==
to check values 您需要
==
来检查值
UPDATE 更新
Based on your comments below here is the jQuery for the same thing: 根据以下您的评论,这是同一件事的jQuery:
$(function(){
$('#selectbox1').change(function(){
if(this.selectedIndex == 2){
$('#input1, #input2, #asterisk').css('visibility', 'visible');
$('#input2').addClass('required');
}
else {
$('input, #asterisk').css('visibility', 'hidden');
$('#input2').removeClass('required');
}
})
})
you could also do this, http://jsfiddle.net/edelman/egjF4/10/ 您也可以这样做, http://jsfiddle.net/edelman/egjF4/10/
var form = document.getElementById('myform');
if (form.selectbox1.selectedIndex == 2)
this way you're caching the form in case you want to reference it later, preventing another element lookup and speeding up your code. 这样,您可以缓存表单,以备日后参考时使用,以防止再次查找元素并加快代码的速度。
I believe jsfiddle runs in it's own little protective XPC bubble, so showhidebtime will not be seen as defined via an inline javascript call. 我相信jsfiddle运行在它自己的保护性XPC气泡中,因此showhidebtime不会被视为通过内联javascript调用定义。 Best practice is to always add events in the javascript file, not inline with the elements.
最佳做法是始终在javascript文件中添加事件,而不是与元素内联。
Also you need to change your form to show name="myform"
in order for document.myform
to work. 另外,您还需要更改表单以显示
name="myform"
才能使document.myform
正常工作。
Try this fiddle: http://jsfiddle.net/garreh/qb6fw/ 试试这个小提琴: http : //jsfiddle.net/garreh/qb6fw/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.