Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取所选值选择2<;李>;不选择_Php_Jquery_Jquery Select2 - Fatal编程技术网

Php 获取所选值选择2<;李>;不选择

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

我使用select2,当我选择一个标记时,我想得到我的选项的值,但是我的select2输出是这样的 “span”“UL”“li”当我在这里搜索时,我发现有人说,您可以像下面的
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
});