使用prototype/Javascript基于单选按钮获取表行

使用prototype/Javascript基于单选按钮获取表行,javascript,html,radio-button,html-table,prototypejs,Javascript,Html,Radio Button,Html Table,Prototypejs,我有一个html表,它有一个名称和一个单选按钮,如下所示: <table id="cars"> <thead> <tr> <th>Car Name</th> <th></th> </tr> </thead> <tbody> <tr> <td class="car">Ford Foc

我有一个html表,它有一个名称和一个单选按钮,如下所示:

<table id="cars">
  <thead>
    <tr>
      <th>Car Name</th>
      <th></th>
    </tr>
  </thead>
  <tbody>

    <tr>
      <td class="car">Ford Focus</td>
      <td><input type="radio" id="selectedCar" name="selectedCar" value="8398"></td>
    </tr>

    <tr>
      <td class="car">Lincoln Navigator</td>
      <td><input type="radio" id="selectedCar" name="selectedCar" value="2994"></td>
    </tr>

  </tbody>
</table>
<input type="button" value="Select Car" onclick="selectCar()"></input>
但是我不能得到正确的值

我使用的是prototype,但解决方案也可以是纯Javascript。

您可以尝试使用以下HTML:

<tr>
  <td class="car">Ford Focus</td>
  <td><input type="radio" id="selectedCar1" name="selectedCar" value="8398"></td>
</tr>

<tr>
  <td class="car">Lincoln Navigator</td>
  <td><input type="radio" id="selectedCar2" name="selectedCar" value="2994"></td>
</tr>
(返回
true
false

或者,如果要获取选定的值,请使用:


元素的ID必须是唯一的@Marcel Korpel那么如果ID必须是唯一的,我如何才能看到哪一个被选中?该表是动态构建的,可能有许多行,因此我需要任何简单的方法来循环它们,以查看哪个行被选中,并获得相应的信息。
<tr>
  <td class="car">Ford Focus</td>
  <td><input type="radio" id="selectedCar1" name="selectedCar" value="8398"></td>
</tr>

<tr>
  <td class="car">Lincoln Navigator</td>
  <td><input type="radio" id="selectedCar2" name="selectedCar" value="2994"></td>
</tr>
val1 = $('selectedCar1').checked;
function getCarValue()
{
    var theCars = document.getElementsByName("selectedCar");
    var i = theCars.length;
    while (i--) {
        if(theCars[i].checked)
             return theCars[i].value;

    }
}