Pregunta

Hola estoy trabajando en este pedazo de código para un carrito de la compra

$('.addtoCart').click(function() {

    //get button id      
    var cartID = $(this).attr("id"); 

    //get check box id   
    var checkBoxID = $('input.cartLevelChecked:checked').attr("id");

    //get parent id
    var levelListID = $(this).closest('li').attr("id");

    if(('#'+cartID && '#'+checkBoxID).parent('#'+levelListID)){
        $(".selectPlan").show();
    }

    //alert(/*cartID + checkBoxID +*/ levelListID);
});

Básicamente estoy comprobando para ver si la casilla de verificación comprueba el usuario y el botón se hace clic en pertenece a la misma matriz a continuación, mostrar un div

cualquier ayuda sería muy apreciada. gracias

¿Fue útil?

Solución

Es necesario comparar levelListID, que consultada correctamente, a la identificación de los padres más cercano li de la casilla de verificación, el cual se debe consultar la misma manera:

if (levelListID === $('#' + checkBoxID).closest('li').attr('id')) {
    ...
}

Otros consejos

Esto debería funcionar:

//compares the parent() HTMLElement (not the jQuery object)
if ( $(this).parent().get(0) == $('input.cartLevelChecked:checked').parent().get(0) ) {
     $(".selectPlan").show();
}

que fueron casi allí, esto debería funcionar si el padre es el padre inmediato de ambos cartID y checkBoxID:

if($('#'+cartID).parent().attr(id) == levelListID && $('#'+checkboxID).parent().attr(id) == levelListID ) {
    $(".selectPlan").show();
}

Usted sólo puede subir a los padres <li> y ver si existe el elemento facturado allí, así:

$('.addtoCart').click(function() {
  if($(this).closest('li').find('input.cartLevelChecked:checked').length) {
    //yes we're in the same parent
  }
});

Esto utiliza .closest() ir al <li> padres luego mira en el interior de la casilla de verificación usando .find() . Entonces estamos comprobando que la .length no es 0 (true) lo que significa que se encontró una marcada .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top