2013-06-29 6 views
0

J-Query에서 작업했는데 옵션 값을 가져 오는 데 문제가 있습니다. 특정 텍스트가있는 옵션을 원합니다 (텍스트는 대소 문자를 구분하지 않습니다). 나는 다음 코드를 사용 -특정 텍스트로 드롭 다운 목록 항목을 가져 오는 방법 (대소 문자를 구분하지 않음)

내 html로 코드입니다 -

<select id="ddlStateList_CA"> 
      <option value="AB">Alberta</option> 
      <option value="BC">British Columbia</option> 
      <option value="MB">Manitoba</option> 
      <option value="NB">New Brunswick</option> 
      <option value="NL">Newfoundland and Labrador</option> 
      <option value="NT">Northwest Territories</option> 
      <option value="NS">Nova Scotia</option> 
      <option value="NU">Nunavut</option> 
      <option value="ON">Ontario</option> 
      <option value="PE">Prince Edward Island</option> 
      <option value="QC">Quebec</option> 
      <option value="SK">Saskatchewan</option> 
      <option value="YT">Yukon</option> 
     </select> 

jQuery 코드 -이 코드를 실행

$('#ddlStateList_CA option:contains("Manitoba")').val(); 

, 내가 올바른 결과를 얻을. 하지만 Jquery 코드를 따라 실행하면 undefined이 나옵니다.

$('#ddlStateList_CA option:contains("manitoba")').val(); 

나는 Manitoba 알려진 manitoba은 동일하지 않습니다,하지만 난 두 경우 이 작업을 수행하려는 경우 내가이 작업을 수행 할 수 있습니까?

Example

+0

또는 연산자가있는 if 문에서. – TGH

답변

1

를 사용하여 필터 내 파일에 내가 가진

$('#button').click(function() { 
    console.log($("select option").filter(function() { 
     return $(this).text().toLowerCase() === 'manitoBa'.toLowerCase(); 
    }).text()); 
}); 

JSFiddle

0

당신이 원하는 것을 할 수있는 기능. 로드 jQuery를 한 후이 추가 :

if(jQuery){ 
    (function($){ 
     //@Author Karl-André Gagnon 
     if(!$.fn.filterText) $.fn.filterText = function(text, caseSensitive){ 
      var returnedObj = $(), 
       caseSensitive = caseSensitive || false, 
       $this = $(this); 
      if(text instanceof Array){ 
       for(var i = 0; i < text.length; i++){ 
        if(typeof text[i] == 'string'){ 
         returnedObj = returnedObj.add($this.filter(function(){ 
          var search = caseSensitive ? text[i] : new RegExp(text[i], 'i') 
          return $(this).text().search(search) != -1; 
         })) 
        }else if(text[i] instanceof RegExp){ 
         returnedObj = returnedObj.add($this.filter(function(){ 
          return $(this).text().search(text[i]) != -1; 
         })) 
        } 
       } 
      }else if(typeof text == 'string'){ 
       returnedObj = returnedObj.add($this.filter(function(){ 
        var search = caseSensitive ? text : new RegExp(text, 'i') 
        return $(this).text().search(search) != -1; 
       })) 
      }else if(text instanceof RegExp){ 
       returnedObj = returnedObj.add($this.filter(function(){ 
        return $(this).text().search(text) != -1; 
       })) 
      } 
      return returnedObj; 
     } 
    })(jQuery) 
} 

축소 된 버전 :http://pastebin.com/hiJ4aw8x

그런 다음, 여러 선택할 수 있습니다. 당신은 그런 식으로 호출 할 수 있습니다 :

alert($('#ddlStateList_CA option').filterText('manitoba', false).val()) 

가있는 "소문자를 구분"대표 false (이 실제로 필요하지 않도록 기본 값은 false입니다).

또는 정규 표현식과 전화 :

alert($('#ddlStateList_CA option').filterText(/manitoba/i).val()); 

바이올린 : http://jsfiddle.net/Cv8uC/6/

1

를 사용하여 간단한 정규식 기반의 필터

var regexp = new RegExp('manitoba', 'i'); 

var val = $('#ddlStateList_CA option').filter(function(){ 
    return regexp.test($(this).text()) 
}).val(); 

데모 : 당신은 아마 모두에 대해 확인해야 Fiddle

+0

잘 작동하지 않습니다. 내 콘솔 출력은 - MB, MB, 정의되지 않음 –

+0

@IshanJain 그것이 어떻게되어 있는지, 첫 번째/p는이 코드에서 나오며 다른 것들은 코드 –

+0

입니다. 알았습니다. 코드가 작동 중입니다. –