这是 JSFiddle 链接:http: //jsfiddle.net/rTJw2/1/
这是HTML:
<label for="gender">Gender: </label>
<input type="radio" name="genderS" id="g1" value="male" checked="checked">Male</input>
<input type="radio" name="genderS" id="g2" value="female" >Female</input>
<br>
<input name="submit" type="submit" value="Select" onclick="scriptResult();"/>
<script type="Javascript">
function scriptResult(){
if (document.getElementById('g1').checked) {
var gender === "male";
} else if (document.getElementById('g2').checked) {
var gender === "female";
};
alert(gender);
};
</script>
我敢肯定这是显而易见的。我刚刚开始学习如何在页面上实现 javascript,我只想在按下提交按钮时运行该函数。
有任何想法吗?