Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取选项从其他选项选择名称选择不带表单标记的名称_Javascript_Html - Fatal编程技术网

Javascript 获取选项从其他选项选择名称选择不带表单标记的名称

Javascript 获取选项从其他选项选择名称选择不带表单标记的名称,javascript,html,Javascript,Html,我有一个简短的代码,用于表单标签 <select id="district" name="lan" class="country_search" onchange="showSubcomune(this.value,this.form.undercomune)"> <option value="0" selected>choose country </option> <option value="10">some c

我有一个简短的代码,用于表单标签

<select id="district" name="lan" class="country_search"
        onchange="showSubcomune(this.value,this.form.undercomune)">
    <option value="0" selected>choose country </option>
    <option value="10">some comue</option>
</select>

<select name="undercomune" id="undercomune" class="country_search">
    <option value="0" selected >chhose comune</option>
</select> 
但是没有人工作

任何帮助都是非常感谢的

编辑:

    function showSubKommun(subCat,selectObj) {
            selectObj.length = 0;


            var j = 0,
            elm;
            for (var k = 0; (elm = kommun[k]); ++k)
            if (elm.lankod == subCat)
                selectObj[j++] = new Option(elm.namn, elm.kommunkod);
        var x = document.getElementById("undercomune");
        var option = document.createElement("option");
        option.text = " \xAB choose comune \u00bb ";
        option.value = 0;
        x.add(option,x[0]); 
        x[0].setAttribute("selected", "selected");
    }

您可以通过
document.getElementById

var elem = document.getElementById('undercomune');
或者如果您正在使用jQuery

elem = $('#undercomune')[0]; // the indexer returns native html element

谢谢你的快速回答。我试过你的,但我得到
selectObj在我的函数中未定义。因此,如果您需要更多信息,我在编辑中添加了我的函数。我是这样使用的
onchange=“showSubKommun(this.value,elem)”
,我当然像您所说的那样定义了elem。@karimdarf为什么要将
.length
设置为0?它不是一个数组,但你使用它的时候就好像它是数组一样。但正如我所说,它对表单标签很有效。只是我想使用它没有表单标签。啊,好的,对不起,它现在可以工作了。抱歉,我现在将javascript代码放在html之后,现在可以工作了:)。在html之前是javascript。
elem = $('#undercomune')[0]; // the indexer returns native html element