jQuery selected = selected
I have <select name="users"> ... </select>
in file chat_select.php.
I refresh this every 8 sekunds to #chatSelect.
This is a refreshing in file chat.php:
var auto_refresh = setInterval(
function () {
$('#chatSelect').load('php/refresh/chat_select.php?hodnota='+$('select.user option:selected').val()).fadeIn();
}, 8000);
What is w开发者_C百科rong? I need that if I choose some value from select that it will be stay (selected=selected).
And what about this?: $("#user option[value=" + val + "]").attr("selected", "selected");
Selected is a property: $('my_select').prop('selected')
will set the property "selected" to true.
http://api.jquery.com/prop/
Save the state of the select box on change by doing
$('#YourSelectBox').change(function() {
var x = this.value;
$(this).data('saved', x);
})
Then to retrieve the saved state by doing
$('#YourSelectBox').data('saved')
Note: refreshing the page will clear any data you have saved.
before each refresh, save the current selected option in a variable, and after refresh is done, use jquery to reselect it
精彩评论