AJAX 动态写入网页内容的问题

kris2010 2011-09-20 01:59:14

<script type="text/javascript" defer="defer">
checkFill(document.getElementById("gp"));
checkAction(document.getElementById("nil"));
</script>


我想通过网页生成后动态插入两张表,两张表是不同的位置
现实情况是,后一个函数的操作会使前一个函数显示的内容消失,
两个函数执行顺序变换一下效果正好相反。

这是什么原因?
并且要怎么实现我想要的效果呢?
...全文
162 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kris2010 2011-09-22
  • 打赏
  • 举报
回复
果真是重名问题,当时懒了,没有一步步跟踪下,犯了个低级错误。
slowhand:多谢。
  • 打赏
  • 举报
回复
技术上肯定可以,但是第一步要把数据拆出来,即你传过来的数据一部分要放表格A,另一部分要放表格B,这个你要先规定好.
然后说到的"现实情况是,后一个函数的操作会使前一个函数显示的内容消失"是否两个表格都被包在一个元素里?表格是直接 ("对象").innerHTML=表格 还是 ("对象").appendChild(表格对象) ?问题不要这么简洁,要让我们猜来猜去就不好了
晓敬 2011-09-21
  • 打赏
  • 举报
回复
onreadystatechage对应的函数查看一下!
APM60- 2011-09-21
  • 打赏
  • 举报
回复
checkAction中的xmlHttp全部换一个名字(如xmlHttp_2),不要和checkFill的重复。

或者都用同步的方法获取xmlHttp.open("Get","rightc.asp?"+transStr,false);
kris2010 2011-09-21
  • 打赏
  • 举报
回复
另,不知道怎么在回复里排版,没有像发帖时的代码标签,所有缩进都没了。
希望这些信息有用,有助于让大家帮助我解决问题。
kris2010 2011-09-21
  • 打赏
  • 举报
回复
先解释下,
checkAction(document.getElementById("nil")) 就是通过java返回的 table 放在
<p id="tbr"></p>标签,
option 的onchange事件同样触发这个函数。

checkFill(document.getElementById("gp"))是通过java设置
<input name="wl" id="wl<%=rs("wl_id")%>" type="checkbox" value="<%=rs("wl_id")%>" onclick="checkAction(this)">
selected 属性,也就是 checkbox的选取。

以下是<body>部分:
<p id="tbr"></p>
<table>
<tr><td colspan="3">用户组:<select id="gp" name="gp" onchange="checkFill(this)">
<option name="gl" value="" selected="selected"></option>
<%
sqlstr="select * from gp_list order by gl_id"
rs.open sqlstr,conn,1,1
if not(rs.bof and rs.eof) then
rs.movefirst
do while not rs.eof
response.write "<option name=""gl"" value=""" & rs("gl_id") & """ >" & rs("gl_name") & "</option>"
rs.movenext
loop
end if
rs.close
%>
</select></td></tr><tr><td> </td></tr>
<%<input name="wl" id="wl<%=rs("wl_id")%>" type="checkbox" value="<%=rs("wl_id")%>" onclick="checkAction(this)"></
sqlstr="select * from web_list order by wl_id"
rs.open sqlstr,conn,1,1
if not(rs.bof and rs.eof) then
rs.movefirst
do while not rs.eof
%>
<tr><td>td><td><%=rs("wl_name")%></td><td><%=rs("wl_dn")%></td></tr>
<%
rs.movenext
loop
end if
call connclose
%>
</table>
<input id="nil" type="hidden" value="">
</body>

checkAction函数部分:
function checkAction(checkBox)
{
var groupId;
var webId;
var transStr;
var now=new Date();
var gpl=document.getElementById("gp");
groupId=gpl.options[gpl.selectedIndex].value
webId=checkBox.value;
if(checkBox.checked==true){
transStr="action=1&gid="+groupId+"&wid="+webId+"&t="+now.getTime();
}
else{
transStr="action=2&gid="+groupId+"&wid="+webId+"&t="+now.getTime();
}
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open("Get","rightc.asp?"+transStr,true);
xmlHttp.setRequestHeader("Content-type","text/html");
xmlHttp.send();
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
document.getElementById("tbr").innerHTML=xmlHttp.responseText;
}
}
return 0;
}

checkFill 函数部分:
function checkFill(checkBox)
{
var groupId;
var transStr;
var xmlElm;
var now=new Date();
xmlElm=0;
groupId=checkBox.options[checkBox.selectedIndex].value
transStr="action=3&gid="+groupId+"&t="+now.getTime();
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open("Get","rightf.asp?"+transStr,true);
xmlHttp.setRequestHeader("Content-type","text/html");
xmlHttp.send();
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
//返回值
if(xmlHttp.responseText!=""){
checkEmpty();
var xmlStr=xmlHttp.responseText.split("-");
for(xmlElm in xmlStr){
document.getElementById("wl"+xmlStr[xmlElm]).checked=true;
}
}
else{
checkEmpty();
}
}
}
return 0;
}

function checkEmpty() //checkFill调用
{
var chkl=document.getElementsByName("wl")
for(var x=0;x<chkl.length;x++){
chkl[x].checked=false
}
return 0;
}
liuyilidan 2011-09-20
  • 打赏
  • 举报
回复
是不是你操作的是同一个标签
比如两个表都是放在一个div里面的
你通过document.getElementById("div").innerHTML="html"都显示到同一个DIV里面了
  • 打赏
  • 举报
回复
mark
kris2010 2011-09-20
  • 打赏
  • 举报
回复
沉得太快,顶下,求高手解惑。

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧