jQuery select2 선택 태그 값을 얻습니까?
안녕 친구들 이것은 나의 코드입니다.
<select id='first'>
<option value='1'> First </option>
<option value='2'> Second </option>
<option value='3'> Three </option>
</select>
이것이 제 선택2 코드입니다.
$("#first").select2();
아래는 선택된 값을 얻기 위한 코드입니다.
$("#first").select2('val'); // It returns first,second,three.
다음과 같은 텍스트를 반환합니다.first,second,three
그리고 나는 그것을 얻고 싶습니다.1,2,3
.
텍스트가 아닌 선택 상자 값이 필요하다는 뜻입니다.
$("#first").val(); // this will give you value of selected element. i.e. 1,2,3.
Select 요소를 가져오려면 사용할 수 있습니다.$('#first').val();
선택한 값의 텍스트를 가져오려면 -$('#first :selected').text();
당신의 글을 올려주시겠습니까?select2()
함수코드
$("#first").select2('data')
모든 데이터를 맵으로 반환합니다.
ajax를 사용하는 경우 선택 직후 select의 업데이트 값을 얻을 수 있습니다.
//Part 1
$(".element").select2(/*Your code*/)
//Part 2 - continued
$(".element").on("select2:select", function (e) {
var select_val = $(e.currentTarget).val();
console.log(select_val)
});
크레딧:스티븐-존스턴
이 솔루션을 사용하면 선택한 요소를 잊어버릴 수 있습니다.선택한 요소에 대한 ID가 없을 때 유용합니다.
$("#first").select2()
.on("select2:select", function (e) {
var selected_element = $(e.currentTarget);
var select_val = selected_element.val();
});
간단한 대답은 다음과 같습니다.
$('#first').select2().val()
다음과 같은 방법으로도 쓸 수 있습니다.
$('#first').val()
시도해 보기:
$('#input_id :selected').val(); //for getting value from selected option
$('#input_id :selected').text(); //for getting text from selected option
위의 솔루션이 최신 select2(4.0.13)에서는 작동하지 않았습니다.
jQuery를 사용하면 다음 문서에 따라 이 솔루션을 사용하여 값을 검색할 수 있습니다. https://select2.org/programmatic-control/retrieving-selections
$('#first').find(':selected').val();
$(".element").select2(/*Your code*/)
.on('change', function (e) {
var getID = $(this).select2('data');
alert(getID[0]['id']); // That's the selected ID :)
});
이 바이올린을 보세요.
기본적으로, 당신이 원하는 것은value
당신의option
- 하지만 당신은 항상 당신의 가치를 얻기 위해 노력합니다.select
-노드와 함께.$("#first").val()
.
따라서 옵션 태그를 선택해야 하며 jQuery selector를 활용합니다.
$("#first option").each(function() {
console.log($(this).val());
});
$("#first option")
셀렉트 에브리option
이 아이드를 가진 원소의 자식입니다.first
.
다른 답변들이 제게 맞지 않아서 해결책을 개발했습니다.
용이한 타겟팅을 위해 class="option-item"으로 옵션을 만들었습니다.
HTML:
<select id="select-test">
<option value="5-val" id="first-id" class="option-item"> First option </option>
</select>
그런 다음 선택한 모든 옵션에 대해 속성을 표시하지 않습니다.
CSS:
option:checked {
display: none;
}
이제 SelectBox에 변경사항을 추가하여 속성을 단순 :숨김 속성으로 표시하지 않고 선택한 옵션을 찾을 수 있습니다.
JQuery:
$('#select-test').change(function(){
//value
$('#select-test').find('.option-item:hidden').val();
//id
$('#select-test').find('.option-item:hidden').attr('id');
//text
$('#select-test').find('.option-item:hidden').text();
});
작업 fiddle: https://jsfiddle.net/Friiz/2dk4003j/10/
해결책 :
var my_veriable = $('#your_select2_id').select2().val();
var my_last_veriable = my_veriable.toString();
언급URL : https://stackoverflow.com/questions/19908273/jquery-select2-get-value-of-select-tag
'sourcecode' 카테고리의 다른 글
dplyr:: 하나의 열을 선택하고 벡터로 출력 (0) | 2023.09.11 |
---|---|
추적되지 않은 내용을 추적하는 방법은 무엇입니까? (0) | 2023.09.11 |
AbstractAnnotationConfigDispatcherServlet 확장 시 getServletConfigClasses() vs getRootConfigClasses()이니셜라이저 (0) | 2023.09.11 |
Excel - 조건부 서식 - 셀이 비어 있지 않으며 0과 같습니다. (0) | 2023.09.11 |
Azure Pipeline PowerShell 작업에서 인라인 다중 라인 파워셸 스크립트를 작성하려면 어떻게 해야 합니까? (0) | 2023.09.11 |