Javascript错误更新输入值

Javascript错误更新输入值,javascript,jquery,html,Javascript,Jquery,Html,我正在开发一个HTML页面,它使用Morris.js创建图表。我添加了日历小部件,允许用户选择开始和结束日期,以及一个按钮来更新数据。我意识到,如果我能自动将日期设置为“今天”,这样图表在第一次加载时就已经有数据了,这会很方便。一切正常:日期设置为今天,查询返回有效的JSON,图表显示正确的数据。但是,字段没有更新,我不知道为什么。同样值得注意的是,在按下“空”字段上的按钮后,日期被正确设置,并在随后的输入中显示。以下是相关代码: <script> // inside head ta

我正在开发一个HTML页面,它使用Morris.js创建图表。我添加了日历小部件,允许用户选择开始和结束日期,以及一个按钮来更新数据。我意识到,如果我能自动将日期设置为“今天”,这样图表在第一次加载时就已经有数据了,这会很方便。一切正常:日期设置为今天,查询返回有效的JSON,图表显示正确的数据。但是,
字段没有更新,我不知道为什么。同样值得注意的是,在按下“空”字段上的按钮后,日期被正确设置,并在随后的输入中显示。以下是相关代码:

<script> // inside head tag
  function updateCharts() {
    startDate = document.getElementById("startDate").value;
    endDate = document.getElementById("endDate").value;
    scale = document.getElementById("scale").value;
    console.log("before " + startDate);
    if (startDate == "") {
        console.log("empty start");
        var today = new Date;
        startDate = today.getFullYear() + "-" + 
                    (today.getMonth() + 1) + "-" + // getMonth in range [0,11]
                    5;//today.getDate();
        document.getElementById("startDate").value = startDate;
        console.log("input " + document.getElementById("startDate").value);
    }
    if (endDate == "") {
        console.log("empty end");
        endDate = startDate;
        document.getElementById("endDate").value = endDate;
        console.log("output " + document.getElementById("endDate").value);
    }
    console.log("after " + startDate + " " + endDate);
    var charts = ['providers', 'variance', 'cities', 'convertions', 'unique_ids'];
    var len = charts.length;
    for (var i = 0; i < len; i++) {
        var chartType = charts[i]
        chart(chartType, startDate, endDate, scale);
    }
  }
</script>

如您所见,Javascript在条件之后(正确地?)设置并检索了值,但它没有显示在浏览器中。

尝试将
window.onload=updatechars
替换为
window.onload=updatechars()


请参阅,

这些值似乎是在JSFIDLE中设置的,问题可能来自其他地方。See fiddle:我看到它在fiddle中工作,我不明白控制台如何看到值,但字段却看不到值。我试过Safari、Firefox和Chrome,所以这也不是浏览器的问题。我不知道什么是错误的:(在控制台中查找javascript错误。如果在更新之前发生错误,则不会设置值。为什么使用jQuery标记?我看不到任何错误或警告。@j08691我还尝试了jQuery设置值
$('#startDate').val(startDate)的方法)
把它注释掉后,我忘了我真的删除了它们。
<div id="dates" class="row">
    <p>From: <input type="text" id="startDate" /> 
        To: <input type="text" id="endDate" />
        <select id="scale">
            <option value="0">Hour</option>
            <option value="1">Day</option>
            <option value="2">Month</option>
        </select>
        <button value="Show" onclick="updateCharts()">Show</button>
    </p>
</div>
<script>
    window.onload = updateCharts();
</script>
</body>
before 
index.html (line 41)
empty start
index.html (line 43)
input 2015-11-5
index.html (line 49)
empty end
index.html (line 52)
output 2015-11-5
index.html (line 55)
after 2015-11-5 2015-11-5