我有一个自动完成组合框,其中包含供应商列表,当选择项目时,它会将ID存储在会话和隐藏字段中。
这是我目前的代码:
<script>
(function( $ ) {
$.widget( "ui.combobox", {
_create: function() {
var self = this,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "";
var input = this.input = $( "<input>" )
.insertAfter( select )
.val( value )
.autocomplete({
delay: 0,
minLength: 0,
source: function( request, response ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
response( select.children( "option" ).map(function() {
var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
return {
label: text.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>" ),
value: text,
option: this
};
}) );
},
select: function( event, ui ) {
ui.item.option.selected = true;
self._trigger( "selected", event, {
item: ui.item.option
});
},
change: function( event, ui ) {
if ( !ui.item ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( this ).val( "" );
select.val( "" );
input.data( "autocomplete" ).term = "";
return false;
}
}
}
})
.addClass( "ui-widget ui-widget-content ui-corner-left" );
input.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
this.button = $( "<button type='button'> </button>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.insertAfter( input )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "ui-corner-right ui-button-icon" )
.click(function() {
// close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
input.autocomplete( "close" );
return;
}
// work around a bug (likely same cause as #5265)
$( this ).blur();
// pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
input.focus();
});
},
destroy: function() {
this.input.remove();
this.button.remove();
this.element.show();
$.Widget.prototype.destroy.call( this );
}
});
})( jQuery );
$(function() {
$( "#combobox" ).combobox();
$( "#toggle" ).click(function() {
$( "#combobox" ).toggle();
});
});
</script>
<form method="post">
<p>Choose Supplier:<select name="SupplierDDL" id="combobox">
<input type="hidden" name="SupplierId">
<option value=""></option>
<?php include 'loadSuppliers.php'; ?>
</select><p>
<p>Number of Items:</label><input type="text" name="ItemsQty"/></p>
<input name="SupplierSubmit" type="submit" value='Submit'>
</form>
<script>
$('input[placeholder], textarea[placeholder]').placeholder();
</script>
另外,如果您可以举例说明如何使用jquery进行回发,因为我可能会在我正在尝试创建的应用程序的下一部分中使用它。
先生/女士,您的回答将会非常有用,非常感谢。
答案 0 :(得分:1)
我理解你想要实现的目标,但你到底在哪里被困?你不知道如何在php中分配会话变量吗?
会话通过php存储在服务器级别,jquery无法访问它们(虽然javascript可以访问cookie)。因此,只需发送表单并使用$_SESSION
var
在组合框的change
事件中,你应该做两件事。首先创建一个隐藏字段来添加值,然后将值发送到php脚本,在那里保存会话var;
添加隐藏字段:
$('#yourform').append('<input type="hidden" name="supplier_id" value="' + $(this).val + '" />'); // add hidden field
将数据发送到您的会话保存脚本
$.get('save_session_var.php', {supplier_id: $(this).val});
最后在save_session_var.php:
$_SESSION['supplier_id'] = $_GET['supplier_id'];