You can call Ajax by clicking on select
and pass the id
of select
clicked as parameter in the URL to be requested. This way you can control what information will be sent by ASP with request("id")
.
Create an event click
that will detect the click on select
and send via $.get
the id
of its element. The return of the ASP (we will see later) will return objects with two values each, separated by a comma, where a value will be the value
and the other the text of option
popularize the select
clicked.
On the return of Ajax you convert these objects into JSON array with the method JSON.parse()
, and using $.each
you mount the HTML of option
. After the $.each
just play the HTML mounted into the select
with .html()
.
Other explanations in the codes.
Example of select
with a id
:
<select id="cursos"></select>
Event and Ajax code:
$('select').click(function(){ // evento click
// este if verifica se o select já foi populado
// evitando que ele seja populado mais de uma vez
// ao ser clicado
if(!$('option', this).length){
$(this).prop("disabled", true); // desabilita o select
var id = this.id; // pega o id do select
var url_ = 'pagina.asp?id='+id; // monto a URL do Ajax passando o id como parâmetro para o ASP
$.get(url_, function(data){
// aqui é o retorno do Ajax
data = data.replace(/,$/, ''); // removo a última vírgula que virá
data = JSON.parse('[' + data + ']'); // converto os objetos em array JSON
var opts = ''; // declaro a variável que irá montar o HTML das options
$.each(data, function(index, item) {
// monto as options concatenando os valores de cada objeto
opts += '<option value="'+item.id+'">'+item.campo+'</option>';
});
$('#'+id).html(opts) // envio o HTML montado para o devido select
.prop("disabled", false); // habilita o select novamente
});
}
});
Change pagina.asp
by your ASP page.
Assembling objects in ASP and returning to Ajax:
In the ASP you get the id
sent by Ajax with request("id")
, and according to the value received, you mount the query to the database.
Make the normal loop of the query to the bank and place the response.write
inside this loop assembling the objects that will be sent to Ajax. Example:
<%
conn = conexão com o banco
set rs = conn.execute(aqui a sua query)
if not rs.eof then
while not rs.eof
response.write("{""id"":"""& rs("coluna_id") &""",""campo"":"""& rs("coluna_campo") &"""},")
rs.movenext
wend
end if
rs.close
set rs = nothing
%>
The ASP tab should only have the above code. It is not a normal page
tagged.
What would be sent by Ajax?
– Sam
@sam sent or brought by ajax? Sent an Asp page, brought two fields within an array for me to loop with each in Jquery. It would be popular for a select.
– Rod
It seems to me that your problem is more in creating the array in ASP, because the rest is quite simple.
– Sam
@sam the two. I am searching here how to put the data of a query in the array (ASP), it is difficult to find something. First I have to fix it, then I think about the JQUERY part.
– Rod
This waiting array would be a simple array, like:
array("item 1","item 2",...)
?– Sam
@Sam then, I don’t know how it has to be, to work out there in Jquery.
– Rod
Let’s go continue this discussion in chat.
– Sam
@sam of I found something similar in php, maybe gave me a light.
– Rod
@sam take a look at the chat
– Rod
@sam spoke to you on chat. rs
– Rod