js有几种判断单选按钮是否被选中的方法?

JavaScript06

js有几种判断单选按钮是否被选中的方法?,第1张

js有几种判断单选按钮是否被选中的方法。

如下参考:

1,首先,打开HTML编辑器100年,新的HTML文件,如:索引。html输入问题的代码,如下所示。

2、在index.html中的度<script>标签,输入js代码:

if($("input[type='radio']:checked").val()){

$('body').append('被选中');

}else{

$('body').append('未被选中');

3.当浏览器运行指数,html页面,js输出判断结果是否选择电台,如下列图所示。

$(function(){

    $("li").click(function() {

        $(this).addClass('act')        

    })

})

这样:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>JQuery radio</title>

<script type="text/javascript" language="javascript" src="JavaScript/jquery-1.6.1.min.js" ></script>

<script type="text/javascript">

$(function () {

$("input").click(function () {

if ($(this).attr("checked")) {

alert("选中了")

}

})

})

</script>

</head>

<body>

<input type="radio"/>

</body>

</html>

扩展资料:

注意事项

<form>

<input type="radio" id="radio1" />

<input type="button" οnclick="check()" value="选中" />

<input type="button" οnclick="uncheck()" value="取消" />

</form>

function check() {

//被选中

document.getElementById("radio1").checked = true

//被选中时,可以执行的方法

if(document.getElementById("radio1").checked == true){

  console.log('==')

}

}

function uncheck() {

//不选中

document.getElementById("radio1").checked = false

//不被选中时,可以执行的方法

if(document.getElementById("radio1").checked == false){

  console.log('++')

}

}