2009-08-06 42 views
1

我在製作一個名爲'cost'的單選按鈕變量,並且希望在'Footer'中爲'<>'如何在表格中打印Javascript變量?

請問,有人可以給一些指導如何做到這一點?很多很多的感謝,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> 

回答

0

這將做到這一點:

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); 
     } 
    } 
} 

你需要對你的標記,唯一的變化是添加span標籤周圍的成本與「cost_marker」的ID以便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

+0

這是瘋狂的,非常好的!非常感謝Paul!我花了好幾個小時看着這個。我將能夠從中學習。再次感謝你。湯姆赫伯特 – user151273 2009-08-06 16:33:57