jQuery: Only first of two .click() events runs when doing it using .click, .trigger()

拈花ヽ惹草 提交于 2019-12-12 03:49:48

问题


I think I've seen a lot of similar questions to mine, but I have found no answer so far. I adapted some dependent drop downs found on the web, which work perfectly when clicked by the user: You click on the first list (select) and when you choose an item that has "children", these show on a second drop down and so on. The problem began when I wanted to pass default values. The first list got its value, nu problem. Then, to generate the second drop down I added a .click() event to the corresponding Id. It actually shows the second drop down and also the selected element (passed from the drop down populating function). HOWEVER, once loaded, no matter how many actual clicks or code mimmicking clicks, the second drop down does not run it's click function, it onle lets you select, but does not generate a third drop down, which, when not passing any values, works perfectly.

What could be wrong?

Here's the main JS code, which is located just before the body of the page:

var demo = {};

demo.globals = {
princ: 0,
princ2: 0,
princ3: 0
}

$(document).on('ready', function() {

$(document).on('click', '#lprinc', function() {
     var value = $(this).val();
      if (value==0){
            $('#lprinc2').html('<option value="">----</option>').attr('disabled', true);
            $('#lprinc2').css({ 'visibility': 'hidden'});
      } 
      else{ 
            $('#lprinc2').removeAttr('disabled');
            $('#lprinc2').css({ 'visibility': 'visible'});
            $('#lprinc3').html('<option value="">----</option>').attr('disabled', true);
            $('#lprinc3').css({ 'visibility': 'hidden'});                           
            if (value != demo.globals.princ) {
            demo.checkDropdown('princ', 'princ2', value, val2);
            demo.globals.princ = value;
            } 
    }
});

 if (val1!=''){
            $('#lprinc').trigger('click');
}

$(document).on('click','#lprinc2', function() {
     var value = $(this).val();
      if (value==0){
            $('#lprinc3').html('<option value="">----</option>').attr('disabled', true);
            $('#lprinc3').css({ 'visibility': 'hidden'});
      } 
      else{ 
            $('#lprinc3').removeAttr('disabled');
            $('#lprinc3').css({ 'visibility': 'visible'});       
            if (value != demo.globals.princ2) {
            demo.checkDropdown1('princ2', 'princ3', value, val3);
            demo.globals.princ2 = value;
            }
     }
 });

if (val2!=''){
         $('#lprinc2').trigger('click');
}    
if (val3!=''){
         $('#princ3').val(val3);
}     

}); 

demo.checkDropdown = function(dropDownName, nextDropDown, value, valorpass) {
jQuery.getJSON('/mod/update.php', { id : nextDropDown, value : value, val: valorpass}, function(data) {
    if (!data.error) {
        $('#lprinc2').html(data.list).removeAttr('disabled');
    } else {
        $('#lprinc2').html('<option value="">----</option>').attr('disabled', true);
        $('#lprinc2').css({ 'visibility': 'hidden'});
        $('#lprinc3').html('<option value="">----</option>').attr('disabled', true);
        $('#lprinc3').css({ 'visibility': 'hidden'});
    }
});    
$("select[name='" + nextDropDown + "']").html(dropDownHtml);

}

demo.checkDropdown1 = function(dropDownName, nextDropDown, value, valorpass) {
jQuery.getJSON('/mod/update.php', { id : nextDropDown, value : value, val: valorpass}, function(data) {
    if (!data.error) {
        $('#lprinc3').html(data.list).removeAttr('disabled');
    } else {
        $('#lprinc3').html('<option value="">----</option>').attr('disabled', true);
        $('#lprinc3').css({ 'visibility': 'hidden'});
    }
});     

$('#lprinc3').html(dropDownHtml);   

Any help would be really appreciated. I have tried .one, .trigger(), .triggerHandler(), .click() to no avail. I have been going nuts overt his one...

Added: update.php:

if (!empty($_GET['value'])) {

$value = $_GET['value'];
$valor= $_GET['val'];

try {

    $objDb = new PDO('mysql:host=localhost;dbname=XXXXX', 'XXXXX', 'XXXXX');
    $objDb->exec('SET CHARACTER SET utf8');

    $sql = "SELECT * 
            FROM `categories`
            WHERE `master` = ?";

    $statement = $objDb->prepare($sql);
    $statement->execute(array($value));
    $list = $statement->fetchAll(PDO::FETCH_ASSOC);

    if (!empty($list)) {

        $out = array('<option value="0">Seleccione...</option>');

        foreach($list as $row) {
            $selstrg = "<option value='".$row['id']."' ";
            if ($row['id']==$valor){
                $selstrg = $selstrg." selected ='selected' ";   
            }       
            $selstrg = $selstrg.">".$row['name']."</option>";
            $out[] = $selstrg;          
        }

        echo json_encode(array('error' => false, 'list' => implode('', $out)));

    } else {
        echo json_encode(array('error' => true));
    }

} catch(PDOException $e) {
    echo json_encode(array('error' => true));
}

} else {
echo json_encode(array('error' => true));
}

回答1:


Updated Answer:

I think you need to update the jQuery events to on.

Update code like:

$('#princ2').click(function() {

To:

$('#princ2').on('click', function() {

Also the example at http://eduardoarria.comze.com/ shows the error dropDownHtml is not defined there is a problem with the following line of code:

$("select[name='" + nextDropDown + "']").html(dropDownHtml);

Your attempting to use dropDownHtml which has not been declared or assigned. Remove the lines of code that attempt to set the HTML to dropDownHtml.



来源:https://stackoverflow.com/questions/11388175/jquery-only-first-of-two-click-events-runs-when-doing-it-using-click-trig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!