我想加载表单数据并将其发送到服务器,然后使用返回的数据绘制图表。如果表单的输入字段有默认数据并且我评论它工作正常
//$("button").click(function(){
如果默认文本字段为空,我不会得到任何图表。我尝试了不同的方法,但仍然无法通过单击按钮从表单中加载它。我现在被困了2天。请帮忙。提前致谢。这是代码
<script type="text/javascript">
var chart = null;
var dataString = [];
$(document).ready(function() {
$("button").click(function() {
$(function() {
var city1 = $("#city1").val();
var city2 = $("#city2").val();
dataString = {
city1: city1,
city2: city2
};
requestData();
});
});
function requestData() {
$.ajax({
url: 'array.php',
type: 'POST',
data: dataString,
success: function(point) {
var chartSeriesData = [];
var chartCategory = [];
$.each(point, function(i, item) {
var series_name = item.name;
var series_data = item.data2;
var cagory = series_name;
var series = {
name: series_name,
data: item.data2
};
chartSeriesData.push(series);
chartCategory.push(series_name);
});
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
defaultSeriesType: 'column',
},
title: {
text: 'Real time data from database'
},
xAxis: {
categories: chartCategory
},
yAxis: {
minPadding: 0.1,
maxPadding: 0.1,
title: {
text: 'Value',
margin: 40
}
},
series: chartSeriesData
});
},
cache: false
});
}
});
</script>
这是正文部分
<body>
<div id="container" style="min-width: 200px; height: 400px; margin: 0 auto"></div>
<div id="city_form">
<form name="contact" action="" method="post">
<label for="city1" id="city1_label">First city</label>
<input type="text" name="city1" id="city1" size="30" value="Amsterdam" />
<label for="city2" id="email_label">Second city</label>
<input type="text" name="city2" id="city2" size="30" value="London" />
<button>Click me</button>
</form>
</div>
</body>
</html>