I don't do a lot of AJAXy web development, but when I do, I usually make use of Prototype . I've recently created a form containing a <select> element whose <option> s are dynamically generated via an AJAX request. The problem however, is that a selected option was already in the form. So before the AJAX request, my HTML looked something like this:

<select name="s" id="s">
<option value="val1">Value 1</option>
<option value="val2" selected="selected">Value 2</option>
<option value="val3">Value 3</option>
</select>

We can then use Ajax.Request method to dynamically add items into this list. (Note here that http://example.com/dynamic/options/ would be a server-side script giving us our dynamically generated <option> tags.)

new Ajax.Request('http://example.com/dynamic/options/', {
method: 'post',
onSuccess: function(transport) {
if (transport.responseText)
Element.insert($('s'), transport.responseText);
}
});

Our resulting HTML might look something like the following:

<select name="s" id="s">
<option value="val1">Value 1</option>
<option value="val2" selected="selected">Value 2</option>
<option value="val3">Value 3</option>
<option value="val4">Value 4</option>
<option value="val5">Value 5</option>
<option value="val6">Value 6</option>
</select>

One problem that arises, is that once the above <option>s are inserted into the original <select> element, the originally selected option is no longer selected. (In my experiments using Firefox 3.5 on OS X, the last item ― in this case, Value 6 ― becomes selected).

That's not so bad, though, because a little more code will just set the correct <option> as selected. The following code looks at all the options until it finds one with the attribute: selected="selected" , and then ... selects it!

var selected_index = 0;
while (!$('s').down(selected_index).defaultSelected) {
selected_index += 1;
}
$('s').selectedIndex = selected_index;

However...one must be careful where one puts this code! You must take into account that the Ajax Request is asynchronous (that's the A in AJAX)!

Here's what NOT to do:

function append_options() {
// Request the dynamically generated options
new Ajax.Request('http://example.com/dynamic/options/', {
method: 'post',
onSuccess: function(transport) {
if (transport.responseText)
Element.insert($('s'), transport.responseText);
}
});
// Make sure the correct on is selected. var selected_index = 0;
while (!$('s').down(selected_index).defaultSelected) {
selected_index += 1;
}
$('s').selectedIndex = selected_index;
}
document.observe('dom:loaded', function() {
append_options();
});

The Ajax request is sent, but the portion of the code that selects the option containing selected="selected" may get run before there is a response to that Request. Therefore, the selected option gets set, then the dynamically generated options get inserted. This is not what we want!

The proper way to do this is to use the onComplete callback in Ajax.Request. This will ensure that the code to select the appropriate option is run after the Request is completed.

function append_options() {
// Request the dynamically generated options
new Ajax.Request('http://example.com/dynamic/options/', {
method: 'post',
onSuccess: function(transport) {
if (transport.responseText)
Element.insert($('s'), transport.responseText);
},
onComplete: function(transport) {
// Make sure the correct on is selected.var selected_index = 0;
while (!$('s').down(selected_index).defaultSelected) { selected_index += 1;}$('s').selectedIndex = selected_index;
}
});
}
document.observe('dom:loaded', function() {
append_options();
});

This is fairly basic AJAX behavior, but for those of us who don't live, breathe, eat, and sleep AJAX, this is an easy mistake to make.

So the moral of this story? Know your tools! It's always good to Know what's going on!

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: AjaxHTMLFirefox
分页:12
转载请注明
本文标题:A for AJAX - OR - Dynamically generating options for a select element.
本站链接:http://www.codesec.net/view/479915.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(56)