gpt4 book ai didi

javascript - HTML 页面重新加载而不更新文本区域的数据

转载 作者:行者123 更新时间:2023-12-03 08:20:55 24 4
gpt4 key购买 nike

在我的HTML page有两个具有默认值的文本区域和一个“提交”按钮。

用户按下“提交”后,我想用新数字更新两个文本区域,同时使用这些值调用函数 loadCountryDataFromCsv(countrySelected, indicatorSelected) .

问题在于整个页面正在重新加载,忽略用户插入的值并使用默认插入到 HTML 正文中的值。

    <html>              

<head>
<meta charset="utf-8">
<title>Homicide count and rate</title>

<script type="text/javascript" src="d3.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {
packages: ["corechart"]
});
google.setOnLoadCallback(drawChart);

function drawChart() {
var range = loadRange();

function loadCountryDataFromCsv(countrySelected, indicatorSelected){

//loadData from CSV and drawChart();
}

function loadRange(){
var minCnt = document.getElementById('minCnt').value;
var maxCnt = document.getElementById('maxCnt').value;
var range = new Array();
range[0] = minCnt;
range[1] = maxCnt;
// var minRt = document.getElementById('minRt');
// var maxRt = document.getElementById('maxRt');
return range;
}

var submit = document.getElementById('submit');
submit.onchange = function(){
// alert(";;;min;;;"+ minCnt+'\n:::max;;;'+maxCnt);
var minCnt = document.getElementById('minCnt').value;
var maxCnt = document.getElementById('maxCnt').value;

range[0] = minCnt; range[1] = maxCnt;

var countrySelected = document.getElementById('country-select').value;
var indicatorSelected = document.getElementById('data-select').value;

// alert(";;;min;;;"+ minCnt+'\n:::max;;;'+maxCnt
//+"\ncountrySelected "+ countrySelected + '\nindicatorSelected ' + indicatorSelected);
loadCountryDataFromCsv(countrySelected, indicatorSelected);
}
}
</script>
</head>





<body>
<div style="margin-left: 5px; width: 100%;">
<d1 style="float: left;"><big>Intentional homicide, counts and rates per 100,000 population</big></d1> <br><br>
<d1 style="float: left;">Intentional homicide is defined as unlawful death inflicted upon a person with the intent to cause death or serious injury.</d1> <br><br>
</div>
<fieldset>
<legend>Select a Country</legend>
<d1>Region: &nbsp; </d1>
<select id="region-select">
<option value="Africa">Africa</option>
<option value="Americas" selected>Americas</option>
<option value="Asia">Asia</option>
<option value="Europe">Europe</option>
<option value="Oceania">Oceania</option>
</select>
<d1>&nbsp;&nbsp;Subregion: &nbsp; </d1>
<select id="subregion-select">
<!-- <option value="" selected>none</option> -->
</select>
<d1>&nbsp;&nbsp;Country: &nbsp; </d1>
<select id="country-select">
</select><br><br>
<d1>Indicator Select: &nbsp; </d1>
<select id="data-select">
<option value="count" selected>count</option>
<option value="rate">rate</option>
</select>
<d1>&nbsp;&nbsp;Format Select: &nbsp; </d1>
<select id="format-select">
<option value="">none</option>
<option value="decimal" selected>decimal</option>
<option value="scientific">scientific</option>
<!-- <option value="percent">percent</option>
<option value="currency">currency</option> -->
<option value="short">short</option>
<option value="long">long</option>
</select>

<form><br>
<d1>Min Range: &nbsp; </d1>
<input style="width: 55px;" id="minCnt" type="text" name="minCnt" value="0"> &nbsp;&nbsp;
<d1>Max Range: &nbsp; </d1>
<input style="width: 55px;" id="maxCnt" type="text" name="maxCnt" value="10000">
&nbsp;&nbsp; <input onclick="return updateRange();" id="submit" type="submit" value="Submit" onsubmit="return false;">
<script type="text/javascript">
function updateRange(){
var minCnt = document.getElementById('minCnt').value;
var maxCnt = document.getElementById('maxCnt').value;

var countrySelected = document.getElementById('country-select').value;
var indicatorSelected = document.getElementById('data-select').value;

loadCountryDataFromCsv(countrySelected, indicatorSelected);

return false;

// var minBox = document.getElementById("minCnt").value;
// var maxBox = document.getElementById("maxCnt").value;
// minBox.value = "new value";
// maxBox.value = "new value";
// console.log(";;;");
}
</script>

</form>
</fieldset>
<div id="chart_div" style="margin-left: -90px; width: 900px; height: 500px;"></div>
</body>
</html>

Pluckr 代码 -> http://plnkr.co/edit/UxcMxzwVShuMjEAo2wug?p=preview

为了停止重新加载页面,我已经尝试使用

<input onclick="return false;" id="submit" type="submit" value="Submit" onsubmit="return false;">

但问题是,虽然页面未加载,但同时我无法调用 loadCountryDataFromCsv(countrySelected, indicatorSelected)使用新值运行。

最佳答案

向您的表单添加一个 ID(例如“form1”)。然后删除submit.onchange =函数并执行以下操作

$('formid').on('submit',function(e){
e.preventDefault();
var minCnt = document.getElementById('minCnt').value;
var maxCnt = document.getElementById('maxCnt').value;
range[0] = minCnt; range[1] = maxCnt;
var countrySelected = document.getElementById('country-select').value;
var indicatorSelected = document.getElementById('data-select').value;
loadCountryDataFromCsv(countrySelected, indicatorSelected);
});

关于javascript - HTML 页面重新加载而不更新文本区域的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33756191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com