du1108 2013-02-25 04:29
浏览 55
已采纳

Jquery live('change',function()不会触发下拉

Anyone can see anything wrong with this code? It is connected to a php function that echos Json data. I am running Jquery 1.9.1. I belive the problem is at the end of the Jquery script, but I can´t find any solution...

var formObject = {
   run : function(obj) {
        if (obj.val() === '') {
       obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
   } else {
       var id = obj.attr('id');
       var v = obj.val();
       jQuery.getJSON('func/blankett_func.php', { id : id, value : v }, function(data) {
            if (!data.error) {
                obj.next('.update').html(data.list).removeAttr('disabled');
            } else {
                obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
            }
        });
    }
}
  };
$(function() {

    $('.update').live('change', function() {
        formObject.run($(this));
    });

});

The Php function:

$id = $_GET['id'];
$value = $_GET['value'];

try {

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

    $sql = "SELECT * 
            FROM `region`
            WHERE `master_id` = ?";
    $statement = $objDb->prepare($sql);
    $statement->execute(array($value));
    $list = $statement->fetchAll(PDO::FETCH_ASSOC);

    if (!empty($list)) {

        $out = array('<option value="">Select one</option>');

        foreach($list as $row) {
            $out[] = '<option value="'.$row['id'].'">'.$row['region'].'</option>';
        }

        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条回答 默认 最新

  • douxia3505 2013-02-25 23:56
    关注

    The problem with the original code was in the .live of the javascript, it should be changed to .on

    The reason for it not triggering was that jQuery changed the API. http://api.jquery.com/on/

    The new jQuery script:

    var formObject = {
    run : function(obj) {
        if (obj.val() === '') {
            obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
        } else {
            var id = obj.attr('id');
            var v = obj.val();
            jQuery.getJSON('func/blankett_func.php', { id : id, value : v }, function(data) {
                if (!data.error) {
                    obj.next('.update').html(data.list).removeAttr('disabled');
                } else {
                    obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
                }
            });
        }
    }
    };
    $(function() {
    
    $('.update').on('change', function() {
        formObject.run($(this));
    });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器