这是我的第一次编码,我试图在我的网站上创建一个功能,从下拉菜单中选择一个项目后,与该项目相对应的值将显示在我创建的输入框中。例如,如果我从下拉菜单中选择“苹果”,则值“ 1.2”和“ 4.00”将分别显示在“磅”和“成本”输入框中。
我已经为下拉菜单和输入框使用HTML和JS编写了代码,并且已经将与项目相对应的数据存储在csv文件中。现在,仅在输入框中显示值,因为我是用JS编写代码的。 HTML文件有什么方法可以从csv文件读取我的数据并在输入框中显示该数据?这是我到目前为止的内容。
<div id="cs-element">
<label id="cs-element-label">Elements</label>
</div>
<select onChange="dropdownTip(this.value)" name="search_type" style="margin-right:10px; margin-top:2px;">
<option selected="selected">Choose Element</option>
<option>Methane</option>
<option>Ethane</option>
<option>Propane</option>
<option>n-Butane</option>
</select>
<div id="result"></div>
<script type="text/javascript">
function dropdownTip(value){
console.log(value);
document.getElementById("myText").value="190.6";
document.getElementById("myText1").value="45.99";
document.getElementById("myText2").value="0.012";
}
</script>
<div id="cs-tc" class="col-sm-4 text-center">
<label id="cs-tc-label" class="control-label">Critical Temperature (K)</label>
<input type="text" id="myText" value=" " class="form-control" name="cs_tc">
</div>
<div id="cs-pc" class="col-sm-4 text-center">
<label id="cs-pc-label" class="control-label">Critical Pressure (atm)</label>
<input type="text" id="myText1" value=" " class="form-control" name="cs_pc">
</div>
<div id="cs-acc" class="col-sm-4 text-center">
<label id="cs-acc-label" class="control-label">Accentric Factor</label>
<input type="text" id="myText2" value=" " class="form-control" name="cs_acc">
</div>
<script>
$(document).ready(function() {
$.ajax({
type: "GET",
url: "B1_1.csv", //read csv file
dataType: "text",
success: function(data) {processData(data);}
});
});
function processData(allText) {
var allTextLines = allText.split("/\r\n|\n/");
var headers = allTextLines[0].split(',');
var lines = [];
for (var i=1; i<allTextLines.length; i++) {
var data = allTextLines[i].split(',');
if (data.length == headers.length) {
var tarr = [];
for (var j=0; j<headers.length; j++) {
tarr.push(headers[j]+":"+data[j]);
}
lines.push(tarr);
}
}
}
</script>
答案 0 :(得分:0)
如果我了解您的要求,则不能,无法从html读取文件。使用html中的表单,您可以上传文件,但该事件由javascript处理。
答案 1 :(得分:0)
答案 2 :(得分:0)
是的,当然可以,您发布的代码就是这样做的,我知道最初可能会造成混淆,但是让我解释一下javascript部分...
<script>
// in html you use script tag to put javascript code inside and using jquery, you // can easily modify the html page.
$(document).ready(function() {
// this is a jquery piece of code that is calling an // ajax request
$.ajax({
type: "GET",
url: "B1_1.csv", //read csv file
dataType: "text",
success: function(data) {processData(data);}
});
});
function processData(allText) {
// a method is a piece of code that you write once because you know that you will // reuse it and it makes the code more readable
var allTextLines = allText.split("/\r\n|\n/");
// split is a method that takes as input a string and (in this case `allText`) //literally split the calling string in n strings storing them in an array. The //strings are splitted by the string you passed as parameter. In this case this //piece of code is simply dividing the entire text in n lines
var headers = allTextLines[0].split(',');
// Now is splitting the line number 1 (look for 0-indexed array on google) in many // strings
var lines = [];
for (var i=1; i<allTextLines.length; i++) {
// A for loop is a loop that repeat itself n-times until a condition is no more
// satisfied, in this case i has to be smaller than the length of that array.
// `var i=1` represents the starting point of the index.
// `i<allTextLines.length` is the condition and `allTextLines.length` returns the
// length of the array.
// In the end `i++` just increment i every time the loop reach the end and restart
var data = allTextLines[i].split(',');
if (data.length == headers.length) {
// an if statement is a piece of code that is executed if and only if a condition
// is satisfied.
var tarr = [];
for (var j=0; j<headers.length; j++) {
tarr.push(headers[j]+":"+data[j]);
}
lines.push(tarr); // The push method add to an array a new element
}
}
}
</script>
我试图尽我所能地解释(我是一个糟糕的老师,所以这对我来说是训练),希望我能对您有所帮助。如果您有任何疑问,请询问,如果您想更多地使用代码,请在网上查找教程,因为有很多东西。也许您有兴趣,我在here年前就学过javascript。