2012-04-16 4 views
0

< 선택 HTML - 여기 내 < 선택 >이있는 값으로 보내려고합니다. jQuery로 보낼 값을 설정했는지 확실하지 않습니다.jQuery 및 다중 선택 옵션

<select id="needapproval" name="needapproval"> 
    <option value="">* * * Select * * *</option> 
    <cfloop query="GetApprover"> 
    <option VALUE="{'theID': '#GetApprover.rar_ID#', 
     'roomname': '#GetApprover.theroom#'}">Request Number - #GetApprover.rar_ID# 
     - Room/Gate Name -#GetApprover.theroom#</option> 
    </cfloop> 
</select> 

OK, 내가 jQuery를에 onchange 이벤트를 할 위에 나는 값을 선택하면. 값은 전송 :

{'theID': '29', 'roomname': 'red room'} 

내가 두 값을 잡고 바르로 설정하는 방법 - 아래 내가 잘못 알고, 내 jQuery를 수 있습니다 :

var theID = $("#needapproval.theID").val(); 
var roomname = $("#needapproval.roomname").val(); 

답변

1

당신은 약간의 수정을하여 그 값을 얻을 수 있습니다 다음과 같은 마크 업

<select id="needapproval" name="needapproval"> 
<option value="">* * * Select * * *</option> 
<cfloop query="GetApprover"> 
<option VALUE='{"theID":"#GetApprover.rar_ID#","roomname":"#GetApprover.theroom#"}'>Request Number - #GetApprover.rar_ID# - Room/Gate Name -#GetApprover.theroom#</option> 
</cfloop> 
</select> 

VALUE 속성에는 작은 따옴표와 큰 따옴표가 있습니다.

값 다음에 JSON.parse()을 입력하면 theIDroomname이됩니다. , 아래를 참조

$(function() { 
    $('#needapproval').change(function() { 
     var selectedVal = $(this).val();   
     var jsonObj = JSON.parse(selectedVal); 
     alert(jsonObj.theID + " " + jsonObj.roomname); 
    }); 
}); 

DEMO