开发者

jquery function include vars from outside, but cant read it

im coding a dynamic set of selects changing the option values with jquery...

here is my code:

function muestra(nombre) {
$("select#" + nombre).html(output.join(''));
$("div#" + nombre).fadeIn('slow');
};   


$("select#fruta").change(function(){

switch ($(this).val()){

    case '1':
        var siguientesValores = {"0":"-- Seleccione --","1":"Nuez Con Cascara","2":"Nuez Sin Cascara"};
        var output = [];
        $.each(siguientesValores, function(key, value) {
          output.push('<option value="'+ key +'">'+ value +'</option>');
        });
        /* The old way... works */
        $('select#tipoproducto').html(output.join(''));
        $("div#tipoproducto").fadeIn('slow');
                    /***/   
        muestra(variedad); // new way, not working
        break;

    case '2': // Almendra > Tipo de Producto
        var siguientesValores = {"0":"-- Seleccione -开发者_开发百科-","3":"Laminadas","4":"Partidas - Rayadas","5":"Almendras con Cascara","6":"Almendras sin Cascara"};
        var otravar = "probando desde fuera de la funcion";
        var output = [];
        $.each(siguientesValores, function(key, value) {
            output.push('<option value="'+ key +'">'+ value +'</option>');
        });
        muestra(variedad);
        break;
        .......

So, as i can see, the output.join('') in the function, cant be passed from the case statement...

So, how can i do these?

Thanks all and sorry for my english..


You are misunderstanding the difference between inline function declaration and standard function declaration I think.

In any programming language, you cannot re-use variables declared in another function. The fact that when you are using an inline function with jQuery is passing all your local variables is because javascript is analyzing your inline-declared variables WITHIN your other function which makes all your local variables as if they would have been global.

Your "muestra" function is declared OUTSIDE your "change" function, whereas your "each" function is WITHIN your "change" function which makes all the "change" local variable accessible to the "each" function but certainly not to the "muestra" one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜