I have an html table that has a name and a radio button like so:
<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>
I want to be able to select a radio button, then click another button and get the value of the radio button (which is a unique ID) as well as the car name text (like Ford Focus). How should I code the selectCar method? I've tried a few things like:
val1 = $('tr input[name=selectedCar]:checked').parent().find('#cars').html();
val1 = $("td input[name='selectedCar']:checked").parents().find('.cars').html();
val1 = $('selectedCar').checked;
but I can't get the proper values.
I'm using prototype, but the solution can be plain Javascript as well.
I have an html table that has a name and a radio button like so:
<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>
I want to be able to select a radio button, then click another button and get the value of the radio button (which is a unique ID) as well as the car name text (like Ford Focus). How should I code the selectCar method? I've tried a few things like:
val1 = $('tr input[name=selectedCar]:checked').parent().find('#cars').html();
val1 = $("td input[name='selectedCar']:checked").parents().find('.cars').html();
val1 = $('selectedCar').checked;
but I can't get the proper values.
I'm using prototype, but the solution can be plain Javascript as well.
Share Improve this question edited Jan 21, 2017 at 21:08 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 8, 2010 at 16:42 David BuckleyDavid Buckley 9817 gold badges17 silver badges30 bronze badges 2- IDs of elements have to be unique! – Marcel Korpel Commented Jun 8, 2010 at 17:03
- @Marcel Korpel So if the ID has to be unique, how can I see which one is checked? The table is built dynamically and could have many rows so I need any easy way to loop through them to see which one was checked and get the corresponding info. – David Buckley Commented Jun 8, 2010 at 17:05
1 Answer
Reset to default 3All IDs have to be unique!
You can try with this 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>
After this, you can test using:
val1 = $('selectedCar1').checked;
(returns true
or false
).
Or, if you want to grab the selected value, use getElementsByName:
function getCarValue()
{
var theCars = document.getElementsByName("selectedCar");
var i = theCars.length;
while (i--) {
if(theCars[i].checked)
return theCars[i].value;
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744840261a4596516.html
评论列表(0条)