2012-12-19 56 views
0

我试图访问组合框的选定选项的文本属性。以下代码是我到目前为止的地方,但我无法识别我的代码中的错误。无法获取组合框的值

<!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> 

我打破了我的脑袋,试图找出哪里出了问题。可以请某人帮我一把。多谢了!

+0

'getSelectedText(elementId)'返回选定的选项的文字为空:'getSelectedText(ovlName);'。你期望看到这个价值在哪里? –

+0

@ValeraLeontyev:我将它分配给一个变量为var textOfOvl = getSelectedText(ovlName);'。没有变化。 –

回答

3

的GET选定的文本应该是这样的,以获得显示的文本:

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

我建议你检查出基本的dijit教程的document.getElementById将产生一个非常不同的reult比的dijit。当与迪杰斯一起工作时。

这也是你输入一个小幅盘整

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

还我建议你使用

dojo.byId('textId'); 

代替的document.getElementById

1

你的代码非常奇怪。正如我可以在在线

理解

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

value属性必须设置为stateSelect,而不是overlayName

而且看到浏览器变更实施submitEntries功能的任何工作结果:

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

working modification of your code