jQuery addOption and selectOptions

2019-07-28 22:47发布

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!

3条回答
够拽才男人
2楼-- · 2019-07-28 23:31

use Jquery Append to add options like this

$("yourid/class here").append($("<option></option>").attr("value", youroption-value).text(youroption-text));
查看更多
姐就是有狂的资本
3楼-- · 2019-07-28 23:32

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/

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-07-28 23:38

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.

查看更多
登录 后发表回答