0

我很难理解这一点。我有这个位代码:

document.getElementById("widget-container-1").innerHTML="<div class='title'>Select town: "+
         "<select id='drop'>"+
         "<option name='Auckland' value='Auckland'>Auckland</option>"+
         "<option name='Christchurch' value='Christchurch'>Christchurch</option>"+
         "<option name='Dunedin' value='Dunedin'>Dunedin</option>"+
         "<option>Hamilton</option>"+
         "<option>Tauranga</option>"+
         "<option>Wellington</option>"+
         "</select><br />"+      
         "<button id='update' name='update' onClick=\"this, _refreshWeatherDisplay(test);'\">update</button></div>"+
         "<div class='monitor'>Sort by"+
         "<input id='radio' name='input' type='radio' value='0' >town"+
         "<input id='radio' name='input' type='radio' value='1' >max temp"+
         "<div class='section'>test</div></div>";        

        select = document.getElementById("drop");
         select.onchange = function(){
            _checkNewTown(this.options[this.selectedIndex].text);
         };

我想获取数据并执行另一个功能:

var _checkNewTown =  function(ntown){
    alert(this.selectedIndex);
}

但是,在 -checkNewTown 函数中保持返回未定义。当我尝试提醒它时,该按钮不起作用。

var _refreshWeatherDisplay = function(weather_info){
 alert("test");
}

我知道这可能很简单,但我不知道我做错了什么如果有人可以帮助我,将不胜感激。

4

1 回答 1

0

您的代码中有很多语法错误,请查看浏览器的控制台以跟踪这些错误(IE9 和 Chrome 为 F12,Firefox 为 Ctrl+Shift+J)。这段代码对我来说很好用:

document.getElementById("widget-container-1").innerHTML = "<div class='title'>Select town: "+
    "<select id='drop'>"+
    "<option name='Auckland' value='Auckland'>Auckland</option>"+
    "<option name='Christchurch' value='Christchurch'>Christchurch</option>"+
    "<option name='Dunedin' value='Dunedin'>Dunedin</option>"+
    "<option>Hamilton</option>"+
    "<option>Tauranga</option>"+
    "<option>Wellington</option>"+
    "</select><br />"+      
    "<button id='update' name='update' onClick=\"_refreshWeatherDisplay('test');\">update</button></div>"+
    "<div class='monitor'>Sort by"+
    "<input id='radio' name='input' type='radio' value='0' >town"+
    "<input id='radio' name='input' type='radio' value='1' >max temp"+
    "<div class='section'>test</div></div>";        
select = document.getElementById("drop");
select.onchange = function(){
    _checkNewTown(select.options[select.selectedIndex].value);
};

你的功能:

var _checkNewTown =  function(ntown){
    alert(ntown);
};
var _refreshWeatherDisplay = function(weather_info){
    alert("test");
};
于 2013-03-31T00:05:44.907 回答