2014-11-20 5 views
0

JSP에서 다음 드롭 다운이 있습니다.jquery를 사용하여 드롭 다운에서 첫 번째 옵션을 감지하는 방법

<s:select name="select_group_id" list="act_list_group" 
    listKey="group_id" listValue="group_name" headerValue="select_group_id" 
    style="width: 130px" id="ms" multiple="true"/> 

드롭 다운의 첫 번째 옵션을 클릭하면 발견하고 싶습니다. 다음과 같이 시도했지만 작동하지 않는 것 같습니다.

$(function() { 
    if(!$("#ms option:selected").length) { 
     $("#ms option").attr("selected", "selected"); 
    } 

    $('#ms').change(function() { 

     $('#ms option:first').click(function(){ 
      //If user clicked the first(top) option in select box 
      alert('first'); 
     }); 

     if(!$("#ms option:selected").length) { 
      $("#ms").val($("#ms option:first").val()); 
      var selectedValue = $('#ms').val(); 
      var selectedText = $("#ms option[value='"+ selectedValue +"']").text(); 
      $(".ms-choice span").text(selectedText); 
     } 
     console.log($(this).val()); 
    }).multipleSelect({ 
     width: '130px', 
     selectAll: false 
    }); 
}); 

의견, pls. 미리 감사드립니다.

답변

1

당신은 일치하는 요소 중 특정 요소에 대한 .index()

검색을 사용할 수 있습니다.

코드

var options = $("#ms option"); 
if(options.index($("#ms option:first") == options.index($("#ms option:selected")) 
{ 
    //First one is selected 
} 
+0

의견을 보내 주셔서 감사합니다. 사용자가 ** 선택 **에서 첫 번째 (맨 위) 옵션을 클릭하고 모든 옵션이 선택되지 않은 경우 (선택 사항이 모두 선택된 경우) 선택된 항목을 제거하면 기꺼이됩니다. 그러나 사용자 클릭을 감지하는 방법이 첫 번째 옵션인지는 잘 모릅니다. –

0
<select id="ms"> 
    <option> First</option> 
    <option> Second</option> 
    <option> Third</option> 
</select> 

jQuery를 코드

$("#ms option:first").click(function(){ 
    alert("First option clicked"); 
}); 

JS Fiddle

관련 문제