根据javascript值显示文本或标签

时间:2018-01-29 15:38:45

标签: javascript html

这是我第一次尝试使用javascript并尝试根据另一个值在文本框中显示值。

以下是我所拥有的内容,我已根据答案进行了更新,但仍无效,

sql数据看起来像:

avg_price评估 933.18 -99.39 1,024.50 61.48 948.00 13.87 939.92 78.56 907.89 20.84 840.73 -0.31 759.06 -16.71 718.83 -14.14 738.08 66.10

<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>

2 个答案:

答案 0 :(得分:1)

首先,您在输入元素上缺少结束标记(>)。

其次,不要传递值,而是尝试传递event并从中获取值,如下所示:

工作代码段

&#13;
&#13;
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;
&#13;
&#13;

答案 1 :(得分:1)

没有标识为foreach($page->comments as $comment): $message = $comment['message']; $messageID = $comment['comment_id']; endforech;的元素,但您要完成的内容如下:

&#13;
&#13;
currentval
&#13;
function updateRecommendation(value) {
  var currenteval = parseInt(value);
  if (currenteval > 0) {
    document.getElementById("evalresults").value = "Sell";
  } else {
    document.getElementById("evalresults").value = "Buy";
  }

};
&#13;
&#13;
&#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>