【问题标题】:onclick event on dropdown is not getting called下拉菜单上的 onclick 事件没有被调用
【发布时间】:2013-04-12 11:10:39
【问题描述】:

我正在将以下组合框“选择页面”动态添加到 jsp 页面,该页面在更改时会填充另一个名为“对象”的组合框。选择页面下拉列表正在正确加载内容,但不会触发其上的事件。即填充对象。我一直在 populateObjects() 函数中保持警报,这表明它甚至没有调用此警报。请帮忙。

//Select Page
       var cell4 = row.insertCell(3);
       var element4 = document.createElement("select");
       element4.setAttribute('id', 'selPageRow' + rowCount);
       element4.setAttribute('name', 'selPageRow' + rowCount);
       element4.setAttribute('onClick', 'javascript:populateObjects(this.options[this.selectedIndex].innerHTML,'+rowCount+');');
       var PageArray = getPages();
       var option = document.createElement("option");
       option.text = "Select..."; 
       option.value = "select"; 
       element4.options.add(option);
       for(var i=0;i<PageArray.length;i++)
           {
               var option = document.createElement("option");
               option.text = PageArray[i].attributes[0].nodeValue; 
               option.value = PageArray[i].attributes[0].nodeValue;  
               element4.options.add(option);
           }
       cell4.appendChild(element4); 

// Code for populating Object dropdown

    function populateObjects(selectedValue,rowCount)
    {
        alert(selectedValue);
        var SelBox = document.getElementById('selObjRow' + rowCount);
        removeAllOptions(SelBox);
        var ObjArry = getObjects(selectedValue);

        var option = document.createElement("option");
        option.text = "Select..."; 
        option.value = "select"; 
        SelBox.options.add(option);
        if(ObjArry.length>0)
        {
            for(var i=0;i<ObjArry.length;i++)
               {
                   var option = document.createElement("option");
                   option.text = ObjArry[i].attributes[0].nodeValue; 
                   option.value = ObjArry[i].attributes[0].nodeValue;  
                   SelBox.options.add(option);
               }
        }
        else
        {
            var option = document.createElement("option");
            option.text = "None"; 
            option.value = "none"; 
            SelBox.options.add(option);
        }
        cell5.appendChild(SelBox);
    }

【问题讨论】:

  • 请提供fiddle 与您的代码
  • 对不起,这是什么小提琴?
  • rowCount 设置在哪里?据我所知,你从来没有定义它。
  • 好的,让我更清楚我的问题。此代码适用于我朋友的系统,用于在选择页面更改时填充对象。尽管它们具有相同的 IE 版本,但它对我不起作用。 IE 设置中是否有任何调整以使 javascript 工作?
  • 我将您的代码粘贴到fiddleJavaScript 部分,但出现错误:“行未定义”。您能否制作一个包含可运行代码的小提琴,以便我们可以破解它并使其按您的意愿工作?

标签: javascript dynamic drop-down-menu


【解决方案1】:

onClick 事件更改为onChange 事件

   element4.setAttribute('onChange', 'javascript:populateObjects(this.options[this.selectedIndex].innerHTML,'+rowCount+');');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 2022-07-05
    • 2017-08-08
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多