简体   繁体   English

如何在表格中打印Javascript变量?

[英]How to Print a Javascript Variable in a Table?

In a table I'm making I have a Radio Button variable named 'cost' and I want to print the 'cost' variable in the footer of the form in a sentence like 'Make the check out for <>' 在一个表格中,我有一个名为“ cost”的单选按钮变量,我想在表格的页脚中用“为<>签出”这样的句子打印“ cost”变量。

Please, can someone give be some guidance as to how to do it? 拜托,有人可以给些指导怎么做吗? Many many thanks, TEH 非常感谢,TEH

<table id="gradient-style" summary="Throttle Conversion">
    <thead>
        <tr>
            <th scope="col">Existing Throttle</th>
            <th scope="col">Upgrade To</th>
            <th scope="col">Additional Features</th>
            <th scope="col"></th>
            <th scope="col"></th>
            <th scope="col">Upgrade Price</th>
            <th scope="col">Select One</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td><strong><center>29 Functions</td>
            <td><strong><center>Simplex Radio</td>
            <td><strong><center>Duplex Radio</td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td>DT400</td>
            <td>DT402</td>
            <td><center>*</td>
            <td></td>
            <td></td>
            <td>$25.00</td>
            <td><input type="radio" name="cost" value="25"></td>
        </tr>
        <tr>
            <td>DT400</td>
            <td>DT402R</td>
            <td><center>*</td>
            <td><center>*</td>
            <td></td>
            <td>$50.00</td>
            <td><input type="radio" name="cost" value="50"></td>
        </tr>
        <tr>
            <td>DT400</td>
            <td>DT40D</td>
            <td><center>*</td>
            <td></td>
            <td><center>*</td>
            <td>$65.00</td>
            <td><input type="radio" name="cost" value="65"></td>
        </tr>
        <tr>
            <td>DT400R</td>
            <td>DT402R</td>
            <td><center>*</td>
            <td><center>*</td>
            <td></td>
            <td>$25.00</td>
            <td><input type="radio" name="cost" value="25"></td>
        </tr>
          <tr>
            <td>DT400R</td>
            <td>DT402D</td>
            <td><center>*</td>
            <td></td>
            <td><center>*</td>
            <td>$65.00</td>
            <td><input type="radio" name="cost" value="65"></td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
       <td colspan="7">  Based on your selection, the conversion cost is <?php $_post["cost"]?>      </td>
        </tr>
    </tfoot>
</table>

This would do it: 这样做:

var table = document.getElementById('gradient-style');
var inputs = table.getElementsByTagName('input');
var radios = [];
var span = document.getElementById('cost_marker');

for(var x = 0; x < inputs.length; x++) {
    if(inputs[x].type == 'radio' && inputs[x].name == 'cost') {
        inputs[x].onclick = function() {
            span.innerHTML = '$' + parseFloat(this.value).toFixed(2);
        }
    }
}

The only change you need to make to your markup is to add a span tag around the cost with an ID of 'cost_marker' so that Javascript knows where to update the cost: 您需要对标记进行的唯一更改是在成本周围添加一个范围标记,其ID为“ cost_marker”,以便Javascript知道在何处更新成本:

<tr>
    <td colspan="7">
    Based on your selection, the conversion cost is 
    <span id='cost_marker'><?php $_post["cost"]?></span>
    </td>
</tr>

And here's an example of it at work . 这是工作中的一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM