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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题