jquery 设置checkbox,radio标签选中或取消,jquery获取select选中的文本值

一、checkbox的选中或取消

html:


<input type = "checkbox" name = "checkbox" >

js

// 选中
$ ( '[name="checkbox"]' ).prop ( 'checked' , true ) // 取消选中设置为false即可


效果:


二、radio的选中或取消

html:

扫描二维码关注公众号,回复: 2378825 查看本文章


<input type = "radio" name = "radio" value = "0" >
<input type = "radio" name = "radio" value = "1" >


1.通过索引选中

js


// 选中第二个,索引从0开始
$ ( '[name="radio"]' ).eq ( 1 ).prop ( 'checked' , true ) // 取消选中设置为false即可


效果



2.通过值选中

js


// 选中值为1的raido
$ ( '[name="radio"][value="1"]' ).prop ( 'checked' , true ) // 取消选中设置为false即可


效果:



三、获取select选中的文本值

html(默认第二个选中)

<select id = "select" >
<option value = "1" >a </option>
<option value = "2" selected >b </option>
<option value = "3" >c </option>
<option value = "4" >d </option>
</select>

js


$ ( '#select option:selected' ).text () // 得到文本b


效果


猜你喜欢

转载自blog.csdn.net/web_xyk/article/details/80545060