javascript innerhtml返回值

时间:2013-03-30 07:13:07

标签: javascript html dom innerhtml

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

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");
}

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

1 个答案:

答案 0 :(得分: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");
};