在jQuery中提交表单的问题

时间:2014-11-25 23:34:49

标签: javascript jquery

尝试使用jQuery提交表单时遇到一些问题。 Y尝试使用这些选项,但它不起作用。

1. Option 1:

$("#servicio").change(function(){
     $(this).closest("#form1").trigger('submit');
});

2. Option 2 

$('select[name="servicio"]').change(function(){
      $(this).parents('form').submit();
});

3.  Option 3

$('#idComboBox').change( function(){
     $(this).closest('form').trigger('submit'); 
});

这是我完整的jQuery代码。我使用php代码绑定选择输入

<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", "Mostrar todos" )
          .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 + " no coincide con ningun elemento" )
          .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() {
    $( "#servicio" ).combobox();
    $("#servicio").change(function(){
      alert("The text has been changed.");   //doesn't works
    });
  });

</script>

HTML code:

<body>

<div align="center">
<form name="form1" id="form1" method="post" action="solicitarcodeSERVICIO.php" target="destino">

  <table>
    <tr>
        <td>        
            <div class="ui-widget">
                <label>Seleccione el Servicio</label>
                <select id="servicio" >                 
                    <?  while($row = mysql_fetch_array($qServicios)) {  ?>
                        <option value="<? echo($row["idServicio"]); ?>"> <? echo($row["nombre"]); ?></option>
                    <? } ?>         
                </select>
            </div>
        </td>   
    </tr>
    <tr>
        <td>
            <input type="submit" value="Seleccionar">
        </td>
    </tr>
  </table>
  </form>
  <p>&nbsp; </p>
  <hr />
  <div align="center"><iframe id="destino" name="destino" width="100%" height="800" frameborder="0"></iframe></div>

</div>

</body>

我已经检查了所有相关帖子,并且在我的情况下从来没有解决方案正常。

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

也许这可以帮到你:

$('#SOMEID').on('change', function() {
  $(this).parent('form').submit();
});

// This is just for testing purposes 
// (please remove it, unless you need it for other reasons)
$('form').on('submit', function() {
  alert('Form submited');
  return false; // prevents the form from submitting
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
  <select id="SOMEID">
    <option>Some Option 1</option>
    <option>Some Option 2</option>
  </select>
</form>

<强> Open with FIDDLE

注意:此答案使用 jQuery 。确保它已加载。