jquery对单选框,多选框,文本框等常见操作小结_javascript技巧

WBOY
Release: 2016-05-16 17:04:51
Original
1188 people have browsed it

一、文本框、单选按钮、复选框、相关操作

复制代码代码如下:

var sex=$("input[name='sex']:checked").val(); //获取一组radio被选中项的值
var item=$("#sel option:selected").text(); //获取select被选中项的文本
var option_num=$('#sel').val(); //获取select项目索引
$("#sel")[0].selectedIndex =1; //select下拉框的第二个元素为当前选中值
$("input[name='sex']").get(1).checked=true; //radio单选组的第二个元素为当前选中值

或者对单选框默认选定设置:
$("input[name='sex']").each(function(){
if($(this).val()==s){
$(this).attr("checked",true);
//this.checked=true;
}
});


Jquery 根据value值设置下拉列表(select)默认选中项
复制代码代码如下:





二、 jQuery获取 Select选择的Text和Value
复制代码代码如下:

语法解释:
$("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项 时触发
var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text
var checkValue=$("#select_id").val(); //获取Select选择的Value
var checkIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值
var maxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值

jQuery设置Select选择的Text和Value:
语法解释:
$("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中
$("#select_id ").val(4); //设置Select的Value值为4的项选中
$("#select_id option[text='jQuery']").attr("selected", true); //设置 Select的Text值为jQuery的项选中

jQuery添加/删除Select的Option项:
语法解释:
$("#select_id").append(""); //为Select追加一个Option(下拉项)
$("#select_id").prepend(""); //为Select插入一个Option(第一个位置)
$("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个)
$("#select_id option[index='0']").remove(); //删除Select中索引值为0的 Option(第一个)
$("#select_id option[value='3']").remove(); //删除Select中Value='3'的 Option
$("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option


应用:
复制代码代码如下:






jQuery common













Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!