jQuery addOption and selectOptions

爷,独闯天下 提交于 2020-01-05 07:23:09

问题


I have to deal with some old Javascript code that is throwing an error at addOption and selectOptions

Error: Object has no method selectOptions

Can someone explain me why is it not working? I am using jQuery 1.3

$("some_id").addOption(nodeId, nodeName); // add to list
$("some_id").selectOptions(/^~~/i, true); // clear selection

I solved the addOption line by this

$("some_id")[0].options.add( new Option(nodeName,nodeId));

but I'm still stuck with selectOptions error.

UPDATE just found out the application is also using Dojo. Could that be the problem? Are these methods Dojo specific?

Thanks!


回答1:


use Jquery Append to add options like this

$("yourid/class here").append($("<option></option>").attr("value", youroption-value).text(youroption-text));



回答2:


try this, you can write your own methods:

$.fn.addOption = function(optText, optValue){
    var option = new Option(optText, optValue);
    return this.append(option);
};

$.fn.selectOption = function(toSelect){
 var $option = this.find("option[value='"+toSelect+"']");    
    if($option.length > 0){  
        //if option with the value passed on found then select it      
        $option.prop("selected","selected");
    }else{
        alert("option not found");
    }
};

var $select = $("#selectOption");
$select.addOption("Dummy1",2);
$select.addOption("Dummy2",3);

$select.selectOption(231);

working fiddle here: http://jsfiddle.net/maverickosama92/rGzPS/1/




回答3:


Finally found whats wrong with it. These methods come from a jquery plugin by TexoTela. Why would someone do that just for select boxes?? Beats me

Thanks everybody for the responses. They taught me something indeed.



来源:https://stackoverflow.com/questions/19358385/jquery-addoption-and-selectoptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!