【问题标题】:join 2 same almost same function into 1 [closed]将2个几乎相同的功能加入1个[关闭]
【发布时间】:2013-10-18 16:29:48
【问题描述】:
函数 showRole(str,x) { 如果 (str=="") { document.getElementById("txtHintrole"+x+"").innerHTML=""; 返回; } 如果(窗口.XMLHttpRequest) {// IE7+、Firefox、Chrome、Opera、Safari 的代码 xmlhttp=新的 XMLHttpRequest(); } 别的 {// IE6、IE5的代码 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=函数() { 如果(xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHintrole"+x+"").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","http://localhost/tes/index.php/form/role/"+str,true); xmlhttp.send(); } 函数 showUser(str,x) { 如果 (str=="") { document.getElementById("txtHint"+x+"").innerHTML=""; 返回; } 如果(窗口.XMLHttpRequest) {// IE7+、Firefox、Chrome、Opera、Safari 的代码 xmlhttp=新的 XMLHttpRequest(); } 别的 {// IE6、IE5的代码 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=函数() { 如果(xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint"+x+"").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","http://localhost/tes/index.php/form/hint/"+str,true); xmlhttp.send(); }

我可以将这 2 个功能加入 1 因为我需要他们两个来显示数据 我不知道如何用这个设置 2 功能

newcell.childNodes[0].setAttribute("onchange","showUser(this.value,"+xx+");");

【问题讨论】:

  • 不看功能很难说。您的控制台是否出现错误?
  • 能否附上(相关的)HTML代码。
  • 我已经编辑了我的帖子,在这个网站说我的帖子包含代码而不是代码之前我无法发布它

标签: javascript jquery


【解决方案1】:

我猜你想在onchange 上调用两个函数。试试这个:

newcell.childNodes[0].setAttribute("onchange",function(){
    showUser(this.value,"+xx+");
    secondFunction();
});

或者既然你标记了这个 jQuery,那就用 jQuery 的方式来做吧:

$(newcell.childNodes[0]).change(function(){
    showUser(this.value,"+xx+");
    secondFunction();
});

【讨论】:

  • 我已经手动尝试过,但我得到了错误,因为我的两个函数都是用来显示数据的,它们几乎相同,只是有点不同,我想我想把这 2 个函数加入 1 所以我只需要调用 1 个函数
猜你喜欢
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多