var s1=document.getElementById('list_Stencil'); 
stencilvalue= s1.options[s1.selectedIndex].value;  如果listbox中没有选中值 页面会报脚本错 
如果没有选中值 执行s1.options[s1.selectedIndex].value 会直接提示报错 
怎么样用来判断listbox中是否有选中值呢? 我想最后的判断是
if(stencilvalue=="")
{
   alert("请选择一项");
}

解决方案 »

  1.   

    if(s1.value=='')
    {
       alert("请选择一项"); 
    }
      

  2.   

    var s1=document.getElementById('list_Stencil'); 
    if(s1.selectedIndex > -1)
    {
        stencilvalue= s1.options[s1.selectedIndex].value; 
    }
      

  3.   

    如果s1.options[s1.selectedIndex].value; 没有选取中的话
    stencilvalue= s1.options[s1.selectedIndex].value; 这一句会报错
      

  4.   

    var count = 0;
    for(i = 0; i < a.length; i++)
            {
                if(a[i].checked)
                {
                    value = a[i].value;
                    count++;
                }
            }
    if(count==0)
    {
        alert('请选择...')
        return;
    }
      

  5.   

        function getSelectedVal(sel) {
            for (var i = 0; i < sel.options.length; i++) {
                if (sel.options[i].selected) {
                    return sel.options[i].value;
                }
            }
            return null;
        }
        function MyClick() {
            var s1 = document.getElementById('MainContent_list_Stencil');
            stencilvalue = getSelected(s1);
            if (!stencilvalue) {
                stencilvalue = s1.options[0].value;
            }
        }