简体   繁体   English

无法获取组合框的值

[英]Unable to fetch the value of a combo box

I am trying to access the text property of the selected option of a combo box. 我正在尝试访问组合框所选选项的text属性。 The following code is where I have reached so far but I am not able to recognize the error in my code. 到目前为止,以下代码是我到达的地方,但是我无法识别代码中的错误。

<!DOCTYPE html>
<html >
<head>
<link rel="stylesheet" href="dijit/themes/claro/claro.css">
<style type="text/css">
table,td
{
border: 1px solid black;
}
td
{
height:30px;
vertical-align:center;
padding:15px;
}
.tableClass
{
    border:1px;
    border: 1px solid black;
}
</style>
<script src='dojo/dojo.js' data-dojo-config='parseOnLoad: true'></script>
<script>
//require(["dojo/parser", "dijit/form/FilteringSelect", "dojo/store/Memory"]);

</script>
<script type="text/javascript">
    dojo.require("dijit.form.ComboBox");
    function alterEntries()
        {
            //alert("in");
            document.getElementById('overlayName').value = dijit.byId('stateSelect').value;

            dojo.style("description", {
                display:"block",
            });
            dojo.style("overlayName", {
                display:"block",
            });
        }
        function submitEntries(ovlName,overlayDescription)
        {
            var textOfOvl = getSelectedText(ovlName);
            alert("overlayName " + ovlName + " overlayDescription " + overlayDescription);
        }

        function getSelectedText(elementId) 
        {
            var elt = document.getElementById(elementId);

            if (elt.selectedIndex == -1)
            {
                alert("null");
                return null;
            }

            return elt.options[elt.selectedIndex].text;
        }

    </script>

</head>

<body class="claro">
    <select id="stateSelect" data-dojo-type="dijit.form.ComboBox">
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
        <option value="AZ">Arizona</option>
        <option value="AR">Arkansas</option>
        <option value="CA">California</option>
    </select>
     <input name="overlayName" type="text" value="overlayName" id="overlayName"  style="display:none">
    <textarea name="description" cols="50" rows="3" id="description" style="display:none"></textarea>
    <p>
        <button onclick="alterEntries();">EDIT</button>
        <button onclick="submitEntries(document.getElementById('overlayName').value,document.getElementById('description').value);">SUBMIT</button>
    </p>


</body>

</html>

I am breaking my head trying to figure out what is going wrong.Can someone please hep me out.Many thanks! 我要弄清楚到底是怎么回事了,有人可以帮我吗,非常感谢!

your get selected text should look like this in order to get the displayed text: 您获得的选定文本应如下所示,以便获得显示的文本:

function getSelectedText(el){
     var text = dijit.byId(el).get('displayedValue'),
     value = dijit.byId(el).get('value');
     alert(text);
     return text;
}

I recomend you to check out the basic dijit tutorials, document.getElementById will yield a very different reult than dijit.byId when working with dijits. 我建议您查看基本的dijit教程,当使用dijits时,document.getElementById将产生与dijit.byId截然不同的结果。

this is also a slight correction in your input 这也是您输入中的一点更正

 <input name="overlayName" type="hidden" value="stateSelect" id="overlayName">

also i recomend you to use 我也建议你使用

dojo.byId('textId');

instead of document.getElementById 代替document.getElementById

Your code is very-very strange. 您的代码非常奇怪。 As I can understand it at line 据我所知

<input name="overlayName" type="text" value="overlayName" id="overlayName" style="display:none">

value attribute must be set to stateSelect , but not to overlayName . value属性必须设置为stateSelect ,但不能设置为overlayName

And to see any working result in browser change implementation of submitEntries function: 并查看浏览器更改submitEntries函数实现的任何工作结果:

function submitEntries(ovlName,overlayDescription)
{
    var textOfOvl = getSelectedText(ovlName);
    alert(textOfOvl);
}

See working modification of your code . 请参见对代码的有效修改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM