简体   繁体   中英

Is it possible to get the values of radio button in different pages?

I did not post all the html code but i do give an example HTML.

If i have 4 pages is it possible to get all the values of radio button that is checked per page.

PAGE 1

<table>
<tbody>
<tr class="choices1">
<td align="center"><input id="r1" name="radio" type="radio" value="Stop" /></td>
<td>Stop</td>
</tr>
<tr class="choices2">
<td align="center"><input id="r2" name="radio" type="radio" value="Go" /></td>
<td>Go</td>
</tr>
<tr class="choices1">
<td align="center"><input id="r3" name="radio" type="radio" value="Ready" /></td>
<td>Ready</td>
</tr>
<tr class="choices2">
<td align="center"><input id="r4" name="radio" type="radio" value="Fly" /></td>
<td>Fly</td>
</tr>
</tbody>
</table>

My Code

// this is the code that i'm using to get the value of the radio button that is checked. 
// this code is working for 1 page only.
function findSelection(field) {
    var test = document.getElementsByName(field);
    var q1 = 0, q2 = 0;
    var score = 0;

    for (i=0; i < test.length; i++) {
        if (test[i].checked==true) {     
            return (test[i].value);         
        }
    }
}

    function submit() {
        var choice =  findSelection("radio");
        alert(choice);
        return false;
    }

You can bind an onClick event to the input elements.

<table>
<tbody>
    <tr class="choices1">
        <td align="center">
            <input id="r1" name="radio" type="radio" value="Stop" onclick="myFunction(this);" />
        </td>
        <td>Stop</td>
    </tr>
    <tr class="choices2">
        <td align="center">
            <input id="r2" name="radio" type="radio" value="Go" onclick="myFunction(this);" />
        </td>
        <td>Go</td>
    </tr>
    <tr class="choices1">
        <td align="center">
            <input id="r3" name="radio" type="radio" value="Ready" onclick="myFunction(this);" />
        </td>
        <td>Ready</td>
    </tr>
    <tr class="choices2">
        <td align="center">
            <input id="r4" name="radio" type="radio" value="Fly" onclick="myFunction(this);" />
        </td>
        <td>Fly</td>
    </tr>
</tbody>

The onclick function can call a function to alert the value.

 function myFunction(id) {
    alert(id.value);
}

http://jsfiddle.net/czkzwfgf/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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