组合框的Onchange事件不会发出任何警报

时间:2015-09-25 20:18:39

标签: javascript jquery combobox

这可能听起来是一个愚蠢的问题,但我正试图过去这个...我不知道多少小时。 我在这里有一个 jquery组合框

Combobox代码: -

<script>
  (function( $ ) {
    $.widget( "custom.combobox", {
      _create: function() {
        this.wrapper = $( "<span>" )
          .addClass( "custom-combobox" )
          .insertAfter( this.element );

        this.element.hide();
        this._createAutocomplete();
        this._createShowAllButton();
      },

      _createAutocomplete: function() {
        var selected = this.element.children( ":selected" ),
          value = selected.val() ? selected.text() : "";

        this.input = $( "<input>" )
          .appendTo( this.wrapper )
          .val( value )
          .attr( "title", "" )
          .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
          .autocomplete({
            delay: 0,
            minLength: 0,
            source: $.proxy( this, "_source" )
          })
          .tooltip({
            tooltipClass: "ui-state-highlight"
          });

        this._on( this.input, {
          autocompleteselect: function( event, ui ) {
            ui.item.option.selected = true;
            this._trigger( "select", event, {
              item: ui.item.option
            });
          },

          autocompletechange: "_removeIfInvalid"
        });
      },

      _createShowAllButton: function() {
        var input = this.input,
          wasOpen = false;

        $( "<a>" )
          .attr( "tabIndex", -1 )
          .attr( "title", "Show All Items" )
          .tooltip()
          .appendTo( this.wrapper )
          .button({
            icons: {
              primary: "ui-icon-triangle-1-s"
            },
            text: false
          })
          .removeClass( "ui-corner-all" )
          .addClass( "custom-combobox-toggle ui-corner-right" )
          .mousedown(function() {
            wasOpen = input.autocomplete( "widget" ).is( ":visible" );
          })
          .click(function() {
            input.focus();

            // Close if already visible
            if ( wasOpen ) {
              return;
            }

            // Pass empty string as value to search for, displaying all results
            input.autocomplete( "search", "" );
          });
      },

      _source: function( request, response ) {
        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
        response( this.element.children( "option" ).map(function() {
          var text = $( this ).text();
          if ( this.value && ( !request.term || matcher.test(text) ) )
            return {
              label: text,
              value: text,
              option: this
            };
        }) );
      },

      _removeIfInvalid: function( event, ui ) {

        // Selected an item, nothing to do
        if ( ui.item ) {
          return;
        }

        // Search for a match (case-insensitive)
        var value = this.input.val(),
          valueLowerCase = value.toLowerCase(),
          valid = false;
        this.element.children( "option" ).each(function() {
          if ( $( this ).text().toLowerCase() === valueLowerCase ) {
            this.selected = valid = true;
            return false;
          }
        });

        // Found a match, nothing to do
        if ( valid ) {
          return;
        }

        // Remove invalid value
        this.input
          .val( "" )
          .attr( "title", value + " didn't match any item" )
          .tooltip( "open" );
        this.element.val( "" );
        this._delay(function() {
          this.input.tooltip( "close" ).attr( "title", "" );
        }, 2500 );
        this.input.autocomplete( "instance" ).term = "";
      },

      _destroy: function() {
        this.wrapper.remove();
        this.element.show();
      }
    });
  })( jQuery );

  $(function() {
    $( "#combobox" ).combobox();
    $( "#toggle" ).click(function() {
      $( "#combobox" ).toggle();
    });
  });
</script>
</head>
        <select name="combobox" id="combobox" class="combobox">
          <option value=""></option>
             <?php
              try
              {
                $s = $conn->query("SELECT * FROM testing2");
              }
              catch(PDOException $e)
              {
                echo $e->getMessage();
              }
              while($test = $s->fetch(PDO::FETCH_OBJ))
              {
                ?>  
                <option value="<?php echo $test->indexid; ?>"><?php echo $test->v1; ?></option>
                <?php
              }
              ?>
        </select>

我正在尝试从所选框中获取选择值的值,但我无法得到它: - 这是我为它编写的代码: -

<script type="text/javascript">
$('#combobox').change(function(){
  var val = $("#combobox").val();
  alert(val);
})
</script>

但是,这不会执行。我在这做什么错?

这是组合框的视图来源: -

<select name="combobox" id="combobox" class="combobox">
  <option value=""></option>
  <option value="1">value1</option>
  <option value="2">LoValue1</option>
</select>

2 个答案:

答案 0 :(得分:1)

我猜你这里有一些问题。第"""centinell URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.8/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Add an import: from blog import urls as blog_urls 2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) """ from django.conf.urls import include, url from django.contrib import admin from login.views import * urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^$','login.views.ingresar'), url(r'^usuario/nuevo$', 'login.views.nuevo_usuario'), url(r'^ingresar/$', 'login.views.ingresar'), 行:

125

我通常会选择经过充分测试的解决方案。所以,我会要求你使用Select2这些东西。请看下面的代码段:

this.element.children( "option" ).each(function() {
  if ( $( this ).text().toLowerCase() === valueLowerCase ) {
    $(this).prop("selected", true);
    valid = true;
    return false;
  }
});
$(function () {
  $("#akshay").select2({
    "placeholder": "someplaceholder",
    "width": 200
  });
});

$("#akshay").change(function(){
  alert($(this).val());
});

答案 1 :(得分:-1)

试试这个: -

   this._on( this.input, {
      autocompleteselect: function( event, ui ) {
        var alreadySelected = ui.item.option.selected;
        ui.item.option.selected = true;
        this._trigger( "select", event, {
          item: ui.item.option
        });
        if(!alreadySelected){
           $(this.element).change()
        }
      },
      autocompletechange: "_removeIfInvalid"
    });

jquery.ui设置值但不触发更改事件。

修改

只是添加了一个标志,只有当选中的值与前一个值不同时才触发。