开发者

cannot set selectedIndex off select with javascript

开发者 https://www.devze.com 2022-12-24 02:23 出处:网络
I have this code and I keep getting undefined if I test the selectedIndex. alert(x.selectedIndex); So, setting it is also a problem.

I have this code and I keep getting undefined if I test the selectedIndex.

alert(x.selectedIndex);

So, setting it is also a problem.

Does anyone possibly see what the problem is?

//makes list off tags
function ttyps_select(data,naamsel,selectid, containerid){

if(!ttyps.length){
    jQuery.each(data, function(index, itemData) {  
        ttyps.push( new Tagtype(itemData.tag_id, itemData.tag ));
    });
}
opties = "<option value=\"-1\"></option>\n"; 
for(var i=0; i<ttyps.length; i++) {

    var dfnkey = ttyps[i].tag_id;
    var dfnsel = ttyps[i].tag;

    if (dfnkey==selectid) {
        opties +="<option value="+ttyps[i].tag_id+" SELECTED>"+dfnsel+"</option>\n";

    } else {
        opties +="<option value="+dfnkey+">"+dfnsel+"</option>\n";
    }
}

 $("<select name=\"" + naamsel + "\" size=\"1\" ></select>") 
        .html(opties)
        .change(function(e){
            select_tag(containerid);
        })
        .appendTo("#"+naamsel); 
}

function select_tag(id) {
    var x = $('#frmttypid'+id+' select');
    var ttidx =开发者_JS百科 x.val();
    var tag = getTagtype(ttidx).tag;
    x.selectedIndex=0;
    x.blur();
     if( tag ){
        document.forms['frmtags']['frmtag'+id].value=tag;
     } 
}

thanks, Richard


$('selector') (jQuery) returns an object with array-like collection of matched DOM nodes. Your x variable is an jQuery object, not a reference to any particular <select/> element. use

x[0].selectedIndex

x[0] is a reference to the first DOM node in the jQuery object.

0

精彩评论

暂无评论...
验证码 换一张
取 消