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

JavaScript011

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 doGender(gender) {

if (gender == "男") {

gel("radionan").checked = true

} else {

gel("radionv").checked = true

}

}

扩展资料

JS基础代码:

//定义数组

var pageIds = new Array()

pageIds.push('A')

数组长度

pageIds.length

//shift:删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined

var a = [1,2,3,4,5]

var b = a.shift()//a:[2,3,4,5] b:1

//unshift:将参数添加到原数组开头,并返回数组的长度

var a = [1,2,3,4,5]

var b = a.unshift(-2,-1)//a:[-2,-1,1,2,3,4,5] b:7

//注:在IE6.0下测试返回值总为undefined,FF2.0下测试返回值为7,所以这个方法的返回值不可靠,需要用返回值时可用splice代替本方法来使用。

本文实例讲述了js使用DOM设置单选按钮、复选框及下拉菜单的方法。分享给大家供大家参考。具体实现方法如下:

1.设置单选按钮

单选按钮在表单中即<input

type="radio"

/>它是一组供用户选择的对象,但每次只能选一个。每一个都有checked属性,当一项选择为ture时,其它的都变为false.

先贴上一个例子:

复制代码

代码如下:<script

type="text/javascript">

function

getChoice()

{

var

oForm

=

document.forms["uForm1"]

var

aChoices

=

oForm.camera

for

(i

=

0

i

<

aChoices.length

i++)

//遍历整个单选项表

if

(aChoices[i].checked)

//如果发现了被选中项则退出

break

alert("相机品牌是:"

+

aChoices[i].value)

}

function

setChoice(iNum)

{

var

oForm

=

document.forms["uForm1"]

oForm.camera[iNum].checked

=

true

}

</script>

<form

method="post"

name="uForm1"

action="addInfo.aspx">

相机品牌:

<p>

<input

type="radio"

name="camera"

id="canon"

value="Canon">

<label

for="canon">Canon</label>

</p>

<p>

<input

type="radio"

name="camera"

id="nikon"

value="Nikon">

<label

for="nikon">Nikon</label>

</p>

<p>

<input

type="radio"

name="camera"

id="sony"

value="Sony"

checked>

<label

for="sony">Sony</label>

</p>

<p>

<input

type="radio"

name="camera"

id="olympus"

value="Olympus">

<label

for="olympus">Olympus</label>

</p>

<p>

<input

type="radio"

name="camera"

id="samsung"

value="Samsung">

<label

for="samsung">Samsung</label>

</p>

<p>

<input

type="radio"

name="camera"

id="pentax"

value="Pentax">

<label

for="pentax">Pentax</label>

</p>

<p>

<input

type="radio"

name="camera"

id="others"

value="其它">

<label

for="others">others</label>

</p>

<p>

<input

type="submit"

name="btnSubmit"

id="btnSubmit"

value="Submit"

class="btn">

</p>

<p>

<input

type="button"

value="检测选中对象"

onclick="getChoice()">

<input

type="button"

value="设置为Canon"

onclick="setChoice(0)">

</p>

</form>