2

Ok, this is really a simple question but I am really incompetent at JavaScript.

Basically all I have a form with 2 radio buttons on them.

I need a JavaScript statement which basically says

If radiobutton1 is selected then
document.write ("radiobutton1selected")
else if radiobutton2 is selected then
document.write ("radiobutton2selected")

There are similar questions on here i accept but they are all alot more advanced than what i need.

4

3 回答 3

7

单选按钮html:

<input type="radio" name="radionbutton" value="1" id="button1"/>
<input type="radio" name="radionbutton" value="2" id="button"/>

Javascript:

var button1 = document.getElementById("button1");
var button2 = document.getElementById("button2");

if (button1.checked){
    alert("radio1 selected");
}else if (button2.checked) {
    alert("radio2 selected");
}
于 2012-12-19T17:53:12.013 回答
2

http://jsfiddle.net/Squeegy/KCT8h/

html

<input type="radio" name="zing" id="foo" checked/>
<input type="radio" name="zing" id="bar"/>​

js

if (document.getElementById('foo').checked) {
    alert('foo');
} else {
    alert('bar');
}​
于 2012-12-19T17:53:09.357 回答
0

这很简单:

var button1 = document.getElementById('button1');
var button2 = document.getElementById('button2');
alert(button1.checked?"Button1 is checked":"Button2 is checked");
于 2016-09-24T14:25:49.340 回答