无法使用 javascript 将 selectedIndex 设置为关闭选择

发布于 2024-08-25 21:43:36 字数 1261 浏览 4 评论 0原文

我有这段代码,如果我测试 selectedIndex,我会一直得到未定义的信息。

alert(x.selectedIndex);

所以,设置也是一个问题。

有人可能看出问题是什么吗?

//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 = x.val();
    var tag = getTagtype(ttidx).tag;
    x.selectedIndex=0;
    x.blur();
     if( tag ){
        document.forms['frmtags']['frmtag'+id].value=tag;
     } 
}

谢谢,理查德

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 = x.val();
    var tag = getTagtype(ttidx).tag;
    x.selectedIndex=0;
    x.blur();
     if( tag ){
        document.forms['frmtags']['frmtag'+id].value=tag;
     } 
}

thanks, Richard

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菊凝晚露 2024-09-01 21:43:36

$('selector') (jQuery) 返回一个对象,其中包含匹配的 DOM 节点的类似数组的集合。您的 x 变量是一个 jQuery 对象,而不是对任何特定

x[0].selectedIndex

x[0] 是对 jQuery 对象中第一个 DOM 节点的引用。

$('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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文