jQuery addOption and selectOptions

jQuery addOption and selectOptions

本文关键字:selectOptions and addOption jQuery      更新时间:2023-09-26

我必须处理一些旧的Javascript代码,这些代码在addOptionselectOptions时抛出错误

错误:对象没有方法选择选项

有人可以解释为什么它不起作用吗?我正在使用jQuery 1.3

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

我通过这个解决了添加选项行

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

但我仍然坚持选择选项错误。

更新刚刚发现该应用程序也在使用 Dojo。这可能是问题所在吗?这些方法是否特定于 Dojo?

谢谢!

使用 Jquery Append 添加如下选项

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

试试这个,你可以编写自己的方法:

$.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);

在这里工作小提琴:http://jsfiddle.net/maverickosama92/rGzPS/1/

终于发现了它的问题所在。这些方法来自TexoTela的jquery插件为什么有人会只为选定的盒子这样做?打败我

感谢大家的回复。他们确实教会了我一些东西。