取消表单编辑后,select值问题

ma2jiajia 2010-02-26 03:04:06
Select中的option都没有设置selected,在select控件中添加一自定义属性“default_select”
default_select值是从后台传递过来的,通过JS在onload事件中调用方法
function setSelect() {
var s = document.all.tags("select");
for(var i=0;i<s.length;i++) {
s[i].selectedIndex = s[i].default_select;
}
}

来设置select控件的默认值

但是如果点击表单中的
<input type="reset" name="cancel" id="cancel" value="取消编辑" />
按钮的话,则select中的值都变成第一个option的值了...有什么办法可以避免这种情况吗?
...全文
67 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yixianggao 2010-02-26
  • 打赏
  • 举报
回复
<input type="reset" name="cancel" id="cancel" value="取消编辑" />
不要用 reset,直接用 button
<input type="button" name="cancel" id="cancel" value="取消编辑" />

L@_@K

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>

<body>
<form method="post" action="">
<input type="text" />
<select default_select="1">
<option value="">请选择</option>
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select>
<input type="button" name="cancel" id="cancel" value="取消编辑" />
</form>
<script type="text/javascript">
<!--
function setSelect() {
var s = document.all.tags("select");
for(var i=0;i<s.length;i++) {
s[i].selectedIndex = s[i].default_select;
}
}

setSelect();

document.getElementById("cancel").onclick = function() {
this.form.reset();
setSelect();
};

//-->
</script>
</body>
</html>


Web 开发常用手册

JScript语言参考.rar
http://download.csdn.net/source/308916

DHTML参考手册.rar
http://download.csdn.net/source/308913

样式表中文手册.chm
http://download.csdn.net/source/304124
shan1119 2010-02-26
  • 打赏
  • 举报
回复
in this function (resetFun) reset objects what you want to reset
return false -->
ma2jiajia 2010-02-26
  • 打赏
  • 举报
回复
引用 1 楼 shan1119 的回复:
<input type="reset" name="cancel" id="cancel" value="取消编辑"
onclick="resetFun();return false;"/>

能解释一下这是什么意思吗??
shan1119 2010-02-26
  • 打赏
  • 举报
回复
<input type="reset" name="cancel" id="cancel" value="取消编辑"
onclick="resetFun();return false;"/>

87,996

社区成员

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

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