Php 获取所选值选择2<;李>;不选择
我使用select2,当我选择一个标记时,我想得到我的选项的值,但是我的select2输出是这样的 “span”“UL”“li”当我在这里搜索时,我发现有人说,您可以像下面的Php 获取所选值选择2<;李>;不选择,php,jquery,jquery-select2,Php,Jquery,Jquery Select2,我使用select2,当我选择一个标记时,我想得到我的选项的值,但是我的select2输出是这样的 “span”“UL”“li”当我在这里搜索时,我发现有人说,您可以像下面的jQuery(#service')一样获得您的选项值但我收到一个空警报 html: 你呢? 要在单击时检索列表中当前选定的值,必须在单击事件中使用select2('data'),如: jQuery('.search_icon').on('click', function() { var option_val = j
jQuery(#service')一样获得您的选项值代码>但我收到一个空警报
html:
你呢?
要在单击时检索列表中当前选定的值,必须在单击事件中使用select2('data')
,如:
jQuery('.search_icon').on('click', function() {
var option_val = jQuery("#service").select2('data');
alert(option_val[0].text); //text
//Or
//alert( jQuery("#service option:selected").text() ); //text
//Or
//alert( jQuery("#service").val() ); //value
});
$(“#服务”).val()有什么问题?select 2应更新实际select。直接在click中使用它,而不是在您声明其他值的位置,否则该值在dom readyi获取[object object]警报后不会更新,因为它将返回一个对象,请尝试alert(option_val[0].text)
您也可以使用jQuery(“服务选项:selected”).text()代码>我可以警告值吗?不是文本?jQuery(#service”).val()代码>
<script>
jQuery(document).ready(function() {
jQuery('#service').select2({
minimumInputLength: 1,
width: '100%',
tags: true
});
var option_val = jQuery("#service").select2('data');
jQuery('.search_icon').on('click', function() {
alert(option_val);
});
</script>
jQuery('.search_icon').on('click', function() {
var option_val = jQuery("#service").select2('data');
alert(option_val[0].text); //text
//Or
//alert( jQuery("#service option:selected").text() ); //text
//Or
//alert( jQuery("#service").val() ); //value
});