因此,基本上,我希望有一个小设置,我可以把蔬菜的名字,在输入中,让所有的信息,我需要自动填充到相邻的行,到目前为止,我已经得到了。这可能是一团糟,我不确定。在insertText函数中,我有一些示例文本,而不是至少在第一行工作的whichVeggie函数。没有人需要为我设置任何代码来复制粘贴之类的东西,但是insertText和whichVeggie应该是一个函数吗?我只是失去了任何帮助,将不胜感激。
function addField(n) {
var tr = n.parentNode.parentNode.cloneNode(true);
document.getElementById("tbl").appendChild(tr);
}
function insertText(e) {
if (e.keyCode == 13) {
document.getElementById("sowTime").innerHTML = whichVeggie();
document.getElementById("harvestTime").innerHTML = whichVeggie();
}
}
function whichVeggie() {
var textEntered = document.getElementById("plantNameEntered").innerHTML;
var sowTime = getElementById("sowTime").innerHTML;
var harvestTime = getElementById("harvestTime").innerHTML;
if (textEntered == "tomato") {
sowTime = "100 days";
harvestTime = "200 days";
}
return sowTime && harvestTime;
}<!DOCTYPE html>
<title>Plant Planning Guide</title>
<head>
<link rel="stylesheet" href="index.css" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link
href="https://fonts.googleapis.com/css2?family=Cormorant+Unicase:wght@300&display=swap"
rel="stylesheet"
/>
<h2>When to sow, plant outdoors and harvest in zone 7a</h2>
</head>
<body>
<table id="tbl" style="width: 100%">
<tr>
<th>Plant Name</th>
<th>Time to Sow indoors</th>
<th>Time Until Harvest</th>
</tr>
<tr>
<td>
<input
id="plantNameEntered"
type="text"
name="plantName"
placeholder="Enter Name..."
onkeypress="insertText(event);"
/>
</td>
<td type="text" id="sowTime"></td>
<td type="text" id="harvestTime"></td>
<td>
<input
type="submit"
class="button"
value="Add another plant"
onclick="addField(this);"
/>
</td>
</tr>
</table>
<script src="script.js"></script>
</body>
发布于 2021-03-17 16:53:04
insertText和whichVeggie应该是一个函数吗?
有不同功能的蔬菜是完全没有问题的。我不会像你喜欢的那样写完全正确的代码。
以下是您在JS中需要的几处更正:
获得DOM (如document.getElementById("sowTime").innerHTML; )的
要获得价值,需要使用innerHTML而不是value
若要在函数中返回多个值,则需要使用数组(如返回whichVeggie()[0] )、val2和在需要时提取值(如
瞧!
(我也把它放在解决方案块中,这样就可以快速地帮助其他人,而不是每一条评论:只是为了更好地达到目的)
发布于 2021-03-17 15:23:37
我知道你说你不需要密码,但我认为看到这个可能会对你有帮助。
// Create an object to act as an associative array
var vegetables = [];
vegetables['tomato'] = ['100 days', '200 days'];
vegetables['corn'] = ['120 days', '250 days'];我们删除whichVeggie()函数来代替下面的内容。不用担心查身份证
function insertText(e) {
if (e.keyCode == 13) {
// e.target will point to the input field that triggered this event
// .value will retrieve the text from that field
// Retrieve the vegetable data from the object
v = vegetables[e.target.value];
// Make sure it found a match in the object
if (typeof v !== 'undefined'){
// Remember e.target is the input element. The first parent of
// it will be TD (the cell). The parent of that TD will give us the TR (table row)
// children then give us an array of all TDs in that row.
var c = e.target.parentNode.parentNode.children;
// c[1] references the 2nd TD in the row, c[2] is the 3rd TD
// v[0] and v[1] reference the two values assigned to the specified vegetable above
c[1].innerHTML = v[0];
c[2].innerHTML = v[1];
}
}
}https://stackoverflow.com/questions/66674077
复制相似问题