simulate a click on a select when loading the page

1

I want to simulate a click on a select element that at the moment of loading the page, the images are already loaded, since at this moment I load the select with the groups, but I need it selected by default so that when I open the page the images are loaded. thank you I leave the code

$(nombreProdu());
function nombreProdu(busquedaProdu){
    $.ajax({
        url:'admin/phpadmin/selectProductos.php',
        type: 'GET',
        dataType:'html',
        data : {busquedaProdu: busquedaProdu},
    }) 
    .done(function(result){
        $("#cbx_carrito").html(result);
    })
}

// carga las imagenes al abrir la pagina
$(document).ready(function(){
    $('#cbx_carrito').click(function(){
        var valor = $('#cbx_carrito').val();
        if (valor != null) {
            $('#cbx_carrito').val('1').click(); 
        }
    });
});

$(document).ready(function(){     
    $("#cbx_carrito").change(function () {
        $("#cbx_carrito option:selected").each(function () {
            id_producto = $(this).val();
            $.get("php/carritoproductos.php", { id_producto: id_producto }, function(data){
                $("#cargaCarrito").html(data);
            });            
        });
    })
});
    
asked by jorge enrique chan castillo 06.09.2018 в 00:07
source

3 answers

0

You need to make a trigger with jquery, the syntax would be like this:

    if (valor != null) {
        $('#cbx_carrito').val('1');
        $('#cbx_carrito').trigger("change"); 
    }

I put change, but if you want click just change it by "click".

Another thing, the correct thing would be to only have a $(document).ready so that the code executes correctly.

    
answered by 06.09.2018 / 01:28
source
0

I'll leave you 2 options look which one can help you

1. function by reference

1.1. the functions are created first

function clickcar(){
        var valor = $('#cbx_carrito').val();
        if (valor != null) {
            $('#cbx_carrito').val('1').click(); 
        }
    }
function changecar() {
        $("#cbx_carrito option:selected").each(function () {
            id_producto = $(this).val();
            $.get("php/carritoproductos.php", { id_producto: id_producto }, function(data){
                $("#cargaCarrito").html(data);
            });            
        });
    }

1.2. are assigned to events and are called

$(document).ready(function(){
    $('#cbx_carrito').click(clickcar);
    $("#cbx_carrito").change(changecar);
    clickcar();
    changecar();
});

2. trigguer

2.1. just launch the event

$(document).ready(function(){
    $('#cbx_carrito').click(function(){
        var valor = $('#cbx_carrito').val();
        if (valor != null) {
            $('#cbx_carrito').val('1').click(); 
        }
    });
    $("#cbx_carrito").change(function () {
        $("#cbx_carrito option:selected").each(function () {
            id_producto = $(this).val();
            $.get("php/carritoproductos.php", { id_producto: id_producto }, function(data){
                $("#cargaCarrito").html(data);
            });            
        });
    });
$("#cbx_carrito").trigger("click");
});
    
answered by 06.09.2018 в 01:27
0

The problem was that I have a dynamic select with ajax, it works correctly, what I need is that when I open the page, I will load the images that contain the select option; of the selected option. since at the time of opening the page loaded the option but without the images until I clicked on the select, summarized that simulate the click on the select and load the images of the selected option. I leave the code I hope you serve someone.

$(nombreProdu());
function nombreProdu(busquedaProdu)
{
  $.ajax({
    url:'admin/phpadmin/selectProductos.php',
    type: 'GET',
    dataType:'html',
    data : {busquedaProdu: busquedaProdu},
  })

  .done(function(result){
    $("#cbx_carrito").html(result);//lo que se imprime en la pagina
    $('#cbx_carrito').val('1').trigger('change');// cambia al valor 1, muestra y carga las imagenes cuando abre la pagina.
  })
}



$(document).ready(function(){ // funcion de traer las imagenes segun la opcion seleccionada.
                $("#cbx_carrito").change(function () {
                    $("#cbx_carrito option:selected").each(function () {
                        id_producto = $(this).val();
                        $.get("php/carritoproductos.php", { id_producto: id_producto }, function(data){
                            $("#cargaCarrito").html(data);
                        });            
                    });
                })
            });
    
answered by 18.09.2018 в 04:25