这是我第一次尝试使用javascript并尝试根据另一个值在文本框中显示值。
以下是我所拥有的内容,我已根据答案进行了更新,但仍无效,
sql数据看起来像:
<body>
<?php
$sql = $stmt = $dbo->prepare("SELECT avg_total_price, evaluation FROM view_evm
");
$stmt->execute();
?>
<h1>EVM Details</h1>
<TABLE class="data-table">
<caption class="title">Calculations </caption>
<thead>
<TR>
<TH>Avg Total Price</th>
<TH><input type="text" name="avg_price" id="avg_total_price" </TH>
<TH> </th>
</TR>
<tr>
<TH>Evaluation</TH>
<th><input type="text" name="evaluation" id="evaluation" onkeyup="updateRecommendation(event)"></th>
<th><input type="text" name="evalresults" id="evalresults"</th>
</tr>
</table>
<table id="table_results" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<TH>Avg Total Price</TH>
<th>Evaluation</th>
</tr>
</thead>
<HR>
<?php
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<tbody>
<TR>
<Td><?php echo $row['avg_total_price']; ?> </Td>
<Td><?php echo $row['evaluation']; ?> </Td>
</TR>
<?php
}
?>
</tbody>
</table>
<script>
var table = document.getElementById('table_results');
for (var i=1; i< table.rows.length; i++)
{
table.rows[i].onclick=function()
{
document.getElementById("avg_total_price").value = this.cells[0].innerHTML;
document.getElementById("evaluation").value = this.cells[1].innerHTML;
};
}
function updateRecommendation(event) {
var currenteval = event.target.value;
if (currenteval > 0){
document.getElementById("evalresults").value = "Sell";
} else {
document.getElementById("evalresults").value = "Buy";
}
};
</script>
</body>
</html>
答案 0 :(得分:1)
首先,您在输入元素上缺少结束标记(>
)。
其次,不要传递值,而是尝试传递event
并从中获取值,如下所示:
工作代码段
function updateRecommendation(event) {
var currenteval = event.target.value;
if (currenteval > 0){
document.getElementById("evalresults").value = "Sell";
} else {
document.getElementById("evalresults").value = "Buy";
}
};
&#13;
<tr>
<th>Evaluation</th>
<th><input type="text" name="evaluation" id="evaluation" onkeyup="updateRecommendation(event)"></th>
<th><input type="text" name="evalresults" id="evalresults"></th>
</tr>
&#13;
答案 1 :(得分:1)
没有标识为foreach($page->comments as $comment): $message = $comment['message']; $messageID = $comment['comment_id']; endforech;
的元素,但您要完成的内容如下:
currentval
&#13;
function updateRecommendation(value) {
var currenteval = parseInt(value);
if (currenteval > 0) {
document.getElementById("evalresults").value = "Sell";
} else {
document.getElementById("evalresults").value = "Buy";
}
};
&#13;
请参阅?根据{{1}}输入中输入的值修改<table>
<tr>
<TH>Evaluation</TH>
<TH><input type="number" name="evaluation" id="evaluation" onkeyup="updateRecommendation(this.value);" </th>
<th><input type="text" name="evalresults" id="evalresults" </th>
</tr>
</table>
。