Home >Web Front-end >JS Tutorial >Problems and solutions encountered in js loop dynamic binding function with parameters [transfer]_javascript skills
Problems and solutions encountered in js loop dynamic binding function with parameters [transfer]_javascript skills
WBOYOriginal
2016-05-16 18:16:381128browse
As we all know, binding without parameters is very simple, just use (syntax: "document.getElementById("Object ID name").attachEvent("Event name, such as onchange", function name);") (Example: "document. getElementById("select_0").attachEvent("onchange",modifyFunction);"). (Note: The following is only an example) Binding with parameters is more complicated: document.getElementById("select _0").attachEvent("onchange",function(){modifyFunction (obj,i);); That is, just write the function that needs to be executed in function(). Of course, there is another way to write it: document.getElementById("select _0"). onchange=function(){modifyFunction (obj,i););. Binding successful, OK. However, slowly, we encountered a second problem at this time. The parameter values passed were all the same. It was not as imagined that after passing the value of i, the parameter values of each bound function would be different. So, go online to Baidu. After a difficult search and test, I also found an example as shown below:
This example is to find the component with action through event, then obtain its source, and then extract the name value. In this way, you can get the obj number through the incoming obj, and then perform the corresponding operations. There is just one problem. After this operation, the value of obj has another problem. No matter which select is operated, the value obtained is the last one. Continue to Baidu. Finally, got the reason in one article. The article is reposted as follows: Let’s first look at an example of Javascript using loop binding events: For example: a list of indefinite length, changing the background when the mouse passes over a certain item.
This example loops into a group Object binding event handler function. However, if we add some requirements on this basis. For example, when you click on a certain record, which record does it pop up? Maybe you will write this as a matter of course:
[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute ] 测试一下你会发现alert出来的都是:这是第6记录 其实这里for循环已将整个列表循环了一遍,并执行了i++,所以这里i变成了6, 有什么好的办法解决这个问题吗? 那就是闭包了,个人认为闭包是js中最难捉摸的地方之一, 看看什么是闭包: 闭包时是指内层的函数可以引用存在与包围他的函数内的变量,即使外层的函数的执行已经终止。 这个例子中我们可以这样做:
//在新增按钮上绑定函数 document.getElementById("add").attachEvent("onclick",addFunction); var jc_count = 0;//定义需要改变第几行的值 function txzmcFunction(x,y){//下拉框中绑定的函数 var sql="select txzjc from dm_txzmc where dm='"+x.value+"'";//取得下拉框中的代码,通过ajax获得相应的中文名称 jc_count = y;//定义当前行是第几行 ajaxSelect(sql,"txzjcFunction");//封装的ajax函数 } function txzjcFunction(x){//接收封装的ajax函数返回值,并赋值 document.getElementById("_subarea_hxax_clzjxxb_hxax_txzxxb_update_txzjc_"+jc_count).value=x; } function bb(dx,sz){//解决动态绑定闭包问题要用到函数 this.clickFunc=function(){ txzmcFunction(dx,sz);//调用相应的函数 } } function addFunction(){ //动态循环绑定 var count=document.getElementById("_subarea_hxax_clzjxxb_hxax_txzxxb_update_maxcount").value;//获取最大的行数 for (var i=0;i{ var obj=document.getElementById("_subarea_hxax_clzjxxb_hxax_txzxxb_update_txzmc_" +i); var tp = new bb(obj,i);//解决闭包问题,new一个新的函数类 obj.onchange = tp.clickFunc; } } //显示页面时执行一次 addFunction();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn