>  기사  >  웹 프론트엔드  >  Jquery 작업 Select는 간단하고 편리합니다. js 플러그인이 it_jquery를 처리할 수 있습니다.

Jquery 작업 Select는 간단하고 편리합니다. js 플러그인이 it_jquery를 처리할 수 있습니다.

WBOY
WBOY원래의
2016-05-16 18:41:47897검색

js 코드는 다음과 같습니다.

코드 복사 코드는 다음과 같습니다.

jQuery.fn .size = function()
{
return jQuery(this).get(0).options.length;
}
//선택한 항목의 인덱스 가져오기
jQuery.fn .getSelectedIndex = function( )
{
return jQuery(this).get(0).selectedIndex;
}
//현재 선택한 항목의 텍스트를 가져옵니다
jQuery.fn. getSelectedText = function()
{
if(this.size() == 0)
{
return "드롭다운 상자에 옵션 없음"
}
else
{
var index = this.getSelectedIndex();
return jQuery(this).get(0).options[index].text;
}
}
// 현재 선택된 항목의 값을 가져옵니다
jQuery .fn.getSelectedValue = function()
{
if(this.size() == 0)
{
return "선택한 값 없음 드롭다운 상자에서";
}
else
{
return jQuery(this).val();
}
}
//다음으로 항목 설정 선택하려는 값
jQuery.fn.setSelectedValue = function(value)
{
jQuery(this).get(0).value = value
}
//Set; 선택될 텍스트가 있는 첫 번째 항목
jQuery .fn.setSelectedText = function(text)
{
var isExist = false; var count = this.size(); for(var i=0;i{
if(jQuery(this).get(0).options[i].text == text)
{
jQuery (this).get(0).options[i].selected = true;
isExist = true;
break
}
}
if(!isExist)

alert("드롭다운 상자에 항목이 없습니다." );
}
}
//선택한 인덱스 항목 설정
jQuery.fn.setSelectedIndex = function(index )
{
var count = this.size();
if(index >= count || index < 0)
{
alert("선택한 항목 인덱스가 없습니다. of range");
}
else
{
jQuery (this).get(0).selectedIndex = index;
}
}
//있는지 확인 선택 항목에 값이 있는 항목입니다
jQuery.fn.isExistItem = function(value)
{
var isExist = false
var count = this.size()
(var i=0;i{
if(jQuery (this).get(0).options[i].value == value)
{
isExist = true;
}
}
return isExist;
}
//선택 항목에 항목을 추가합니다. 항목의 값이 이미 존재하는 경우
jQuery.fn.addOption = function(text,value)
{
if(this.isExistItem(value))
{
alert( "추가할 항목의 값이 이미 존재합니다.");
}
else
{
jQuery (this).get(0).options.add(new Option(text,value) );
}
}
//선택 항목에 값이 있는 항목을 삭제합니다. 항목이 없으면 프롬프트
jQuery.fn.removeItem = function(value)
{
if(this.isExistItem(value))
{
var count = this.size()
for(var i=0;i{
if(jQuery(this).get(0).options[i].value == value)
{
jQuery (this).get(0).remove(i)
break;
}
}
}
else
{
alert("삭제할 항목이 존재하지 않습니다!")
}
}
/ /select
jQuery.fn.removeIndex = function(index)
{
var count = this.size()
if(index >= count | | index < 0)
{
alert("삭제할 항목의 인덱스가 범위를 벗어났습니다.")
}
else
{
jQuery(this) .get(0).remove(index);
}
}
//select에서 선택한 항목 제거
jQuery.fn.removeSelected = function ()
{
var index = this.getSelectedIndex();
this.removeIndex(index);
}
//select의 모든 항목 지우기
jQuery.fn.clearAll = function()
{
jQuery(this).get(0).options.length = 0;
}


사용이 매우 간단합니다. 먼저 기본 Jquery .js를 소개합니다.
그런 다음 이것을 가져옵니다. js 파일을 만든 다음 다음 방법을 사용할 수 있습니다.
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.