php obj this,javascript中this.formObj 传递不过去,变成了TypeError: this.formObj is undefined...

var searchForm = {

formObj: null,

jsonUrlAgent: false,

filter: null,

ajaxSelect: null,

regAjaxSelect: function (json) {

this.ajaxSelect = json;

},

init: function (formName, filter) {

this.formObj = document.forms[formName];

this.filter = filter;

if (!this.formObj) {

alert('未在DOM中找到form元素[name=' + formName + ']');

return false;

}

//给select input赋值查询条件

if (this.filter) {

for (var i in this.filter) {

if (/AgentId(P|I)/.test(i) || !this.filter[i]) {

continue;

}

if (/^(input|select)$/i.test(this.formObj[i].tagName)) {

this.formObj[i].value = this.filter[i];

}

}

}

//先给 ajaxSelect 注册的元素获取AJAX数据

if (this.ajaxSelect) {

for (var k in this.ajaxSelect) {

if (this.formObj[k]) {

//已选择的值

if (typeof this.filter[k] == 'undefined') {

this.filter[k] = '';

}

selectNull(this.formObj[k].id);

selectChange(this.ajaxSelect[k][0], this.formObj[k].id, this.filter[k], this.ajaxSelect[k][1], this.ajaxSelect[k][2]);

}

}

}

//再处理层级关系的 AgentIdP AgentIdI

//设定有AgentIdP,一定有AgentIdI

if (this.formObj['AgentIdP']) {

if (this.jsonUrlAgent) {

//已手动定义了JSON地址

} else if (typeof jsonUrl == 'object' && typeof jsonUrl.myAgent == 'string') {

this.jsonUrlAgent = jsonUrl.myAgent;//全局中定义了 jsonUrl

} else {

alert('未配置的 agent 请求JSON 地址!');

return false;

}

if (!this.filter['AgentIdP']) {

this.filter['AgentIdP'] = '';

}

console.log(this.formObj);//有

selectNull(this.formObj['AgentIdP'].id);

selectChange(this.jsonUrlAgent, this.formObj['AgentIdP'].id, this.filter['AgentIdP'], 'AGENTID', 'AGENTNAME', this.reqAgentIdI);

}

},

reqAgentIdI: function () {

console.log(this.formObj);//到了这里就null了

if (this.filter && this.filter['AgentIdI']) {

selectNull(this.formObj['AgentIdI'].id);

selectChange(this.jsonUrlAgent + this.filter['AgentIdP'], this.formObj['AgentIdI'].id, this.filter['AgentIdI'], 'AGENTID', 'AGENTNAME');

}

}

}

用法

HTML部分

渠道选择:

百度

--

游戏名称:

文件名:

用到的函数selectNull 与 selectChange

/**

* @author fzb

* @description 清空select数据的所有option

* @param objId

*/

function selectNull(objId) {

var selObj = document.getElementById(objId);

selObj.options.length = 0;

selObj.options[0] = new Option(' 请选择 ', ' ');

selObj.options[0].className = 'dft-empty';

}

/**

* @author fzb

* @description 用于与selectNull配合自动分析返回JSON数据生成select option值

*

* @param ajaxUrl

* @param subId

* @param dftValue

* @param IdKeyName

* @param TxtKeyName

* @param callbackFunc

* @returns {boolean}

*/

function selectChange(ajaxUrl, subId, dftValue, IdKeyName, TxtKeyName, callbackFunc) {

var subObj = document.getElementById(subId);

if (!subObj) {

alert('不存在的子对象!');

return false;

}

$.getJSON(ajaxUrl, function (json) {

subObj.options.length = 0;

subObj.options[0] = new Option(' 请选择 ', '');

subObj.options[0].className = 'dft-empty';

var j = 1;

for (var i in json) {

subObj.options[j] = new Option(json[i][TxtKeyName], json[i][IdKeyName]);

j++;

}

if (dftValue) {

subObj.value = dftValue;

}

if (callbackFunc) {

callbackFunc();

}

});

}

//-----------------------------------------------------------

javascript部分

var jsonUrl = {"myAgent":"http:\/\/localhost\/index.php\/agent\/jsonMyAgent?pid=","myGame":"http:\/\/localhost\/index.php\/game\/jsonMyGame"};

var filter = {"GameName":"","FileName":"","AgentIdP":"1001","AgentIdI":"1003"};

var ajaxSelect = {

'GameId': [jsonUrl.myGame,'GAMEID','GAMENAME']

};

searchForm.regAjaxSelect(ajaxSelect);

searchForm.init('formSearch',filter);

结果到了 searchForm.reqAgentIdI 就提示报错了

TypeError: this.formObj is undefined

selectNull(this.formObj['AgentIdI'].id);

http://jsbin.com/boriciro/1/

关闭重复问题: javascript 的 callback 是否有问题,会导致对象无法传递 - SegmentFault --依云

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值