js 对象类型 (对象的属性 ,对象的方法) this 关键字

$(function () {

var observation = {

  init: function () {

  this.render();//断点:this bind :function() check_length: function () init: function () render: function () update_checked: function () save_observation: function ()

  this.bind(); //断点:  this bind :function() check_length: function () init: function () render: function () update_checked: function () save_observation: function () date_container:r.fn.init[1] radio:r.fn.init[5]                                        remarks:r.fn.init[1]  submit_btn:r.fn.init[1]

  },

  render: function () {

    window.now = new Date();

    this.submit_btn = $(':button');

    this.radio = $('.type-radio');

    this.date_container = $('#date-picker-date');

    this.remarks = $('#remarks');

  },

  bind: function () {

    var t = this;//t 代表 observation 对象

    this.submit_btn.click(function () {

    t.save_observation(); //如果这里边是this的话 代表不了observation 对象 因此就找不到save_observation()了 this代表当前的按钮 在绑定的事件函数中 this代表绑定的按钮

  });

  this.radio.click(function () {

    t.update_checked(this);

  });

  $('#date-picker-date').date({beginyear: now.getFullYear(), theme: 'datetime'});//初始化date选择器

    this.remarks.bind('input', function () { t.check_length(this);

  });

},

  check_length: function (obj) {//检查备注输入的长度 },

  update_checked: function (obj) { },

  save_observation: function () {//保存数据到服务器 }

};

observation.init();

});

/*
* 添加转诊信息页面js
* import jquery-3.1.0.min.js
* import date.js
* import iscroll.js
* import xhdoctor_mobile.js
* import alertPopShow.js
*/
/* global xhdoctor_mobile */

$(function () {
var hospital_transfer = {
init: function () {
this.render();
this.bind();
},
render: function () {
this.date_container = $('#referralTime');//显示日期的div
this.radio_container = $('.radiocontainer');//radio选项
this.chose_btn = $('.choseBtn');//显示弹窗的按钮
this.popup = $('.popup');//弹窗
this.mask = $('.referralMark');//遮罩层
this.confrimBtn = $('.confrimBtn');//取消按钮
this.checkedBtn = $('.checkedBtn');//确认选择按钮
this.popupContent = $('.popupContent');//弹窗内容显示区域
this.submitBtn = $('.referralBtn');//提交按钮
this.remarks = $('#remarks');
},
bind: function () {
var t = this;
this.date_container.date({theme: "datetime"});//绑定date插件
this.popupContent.on('click', '.radiocontainer', function () {
t.update_checked(t, this);
});
this.chose_btn.bind('click', function () {//显示弹窗
t.show_popup(t, this);
});
this.confrimBtn.bind('click', function () {
t.hide_popup(t);
});
this.checkedBtn.bind('click', function () {
t.update_value(t, this);
});
this.submitBtn.bind('click', function () {
t.submit(t);
});
this.remarks.bind('input', function () {
t.check_length(this);
});
},
check_length: function (obj) {//检查备注输入的长度
var max_length = 200;
var text_length = $(obj).val().length;
if (text_length >= 100) {
$('#titlelen').css('color', 'red');
} else {
$('#titlelen').css('color', 'black');
}
$('#titlelen').html(text_length);

},
update_checked: function (t, obj) {//修改选中的状态
$(obj).parent().find('.radiocontainer').removeClass('checked');
$(obj).addClass('checked');
},
show_popup: function (t, obj) {//显示弹窗
var index = $(obj).attr('data-action');
var has_data = t.popup.eq(index).find('.radiocontainer');
if (has_data.length > 0) {
t.mask.show();
t.popup.eq(index).show();
} else {
if ($(obj).parent().prev().prev().find('button').html() === '请选择') {
var empty_html = $(obj).parent().parent().find('p').eq(index).html();
webToast('请先选择' + empty_html, "middle", 1500);
} else {
webToast('正在加载' + $(obj).parent().prev().html() + ',请稍后', "middle", 1500);
}
}
},
hide_popup: function (t) {//关闭弹窗
t.popup.hide();
t.mask.hide();
},
reset_checked: function (t, index) {//重置选中状态
t.chose_btn.eq(index).html('请选择');
t.popupContent.eq(index).attr('data-action', 0);
},
update_value: function (t, obj) {//修改选中的值
var checked_value = $(obj).parent().parent().find('.popupContent').find('.checked').attr('data-action');
if (checked_value !== undefined) {
var value = $(obj).parent().parent().find('.popupContent').attr('data-action');
if (value !== checked_value) {
$(obj).parent().parent().find('.popupContent').attr('data-action', checked_value);
var index = t.popup.index($(obj).parent().parent());
t.chose_btn.eq(index).html($(obj).parent().parent().find('.popupContent').find('.checked').find('span').html());
t.get_data(checked_value, index);
while (index < 2) {
t.reset_checked(t, index + 1);
index++;
}
}
}
t.hide_popup(t);
},
get_data: function (parent_id, index) {
switch (index + 1) {
case 1:
var data = {area_id: parent_id};
break;
case 2:
var data = {hospital_id: parent_id};
break;
}
if (data) {
$.post("", data, function (res) {
var obj_json = eval("(" + res + ")");
var html = '';
$.each(obj_json, function (keyList, ovalue) {
html += '<div class="radiocontainer" data-action="' + ovalue.id + '"><span>' + ovalue.name + '</span></div>';
$('.popupContent').eq(index + 1).html(html);
});
});
}
},
submit: function (t) {
var data = {
area_id: t.popupContent.eq(0).attr('data-action'),
hospital_id: t.popupContent.eq(1).attr('data-action'),
administrative_id: t.popupContent.eq(2).attr('data-action'),
remarks: $('#remarks').val(),
create_time: $('#referralTime').html().replace(/ - +/g, "-"),
user_id: $('#user_id').val(),
token: $('#token').val()
};
if (data.area_id === 0) {
webToast('请选择转诊城市', "middle", 1500);
return;
}
if (data.hospital_id === 0) {
webToast('请选择转诊医院', "middle", 1500);
return;
}
if (data.administrative_id === 0) {
webToast('请选择转诊科室', "middle", 1500);
return;
}
if (data.remarks.length === 0) {
webToast('请填写备注', "middle", 1500);
return;
}
$.post("/xh_client_h5/save_transfer_treatment", data,
function (res) {
var obj_json = eval("(" + res + ")");
if (obj_json.rev === true) {
xhdoctor_mobile.send_hxmessage('医生转诊', '您的家庭医生已经发起了转诊信息', '/xh_client_h5/user_transfer_treatment/' + obj_json.msg, '/xh_client_h5/show_doctor_transfer_treatment/' + obj_json.msg);
} else {
webToast(obj_json.msg, "middle", 3000);
}
});
}
};
hospital_transfer.init();
});

原文地址:https://www.cnblogs.com/lijiageng/p/6145520.html