ie下的js问题

hbgzg3006 2011-02-21 10:21:12
那位帮我看一些这个问题:ff下没问题,ie下就有问题了。请问为什么?

js是这样写的
[code=JScrip]
function search(){
var context = document.getElementById("context").value;
var type = document.getElementsByName("type");//这里得到的对象貌似没问题
var search = "type=";
for (i in type) {
alert(type[i]); //奇怪的是长度也会被输出
if (type[i].checked == true) { //这里更是undefined,没有checked属性
search += type[i].id;
}
}
search += "&context=" + context;
alert(search);
//window.open("http://localhost:8080/search.jsp?" + search );
}

[/code]

div是这样实现的:
[code=HTM]
<div class="search fl">
<ul>
<li class="shuru"><input name="" type="text" id ="context" value=""/></li>
<li><input name="type" type="radio" value="" id = "title"/> 标题 <input name="type" type="radio" id="author"/> 作者 <input name="type" type="radio" id="content"/> 文章内容</li>
<li style="text-align:right"><input name="" type="image" value="" onClick="search();" /></li>
</ul>
</div>
[/code]
...全文
134 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
<li> <input name="type" type="radio" value="" id = "title"/>

FF允许用这些特殊名字来命名元素
但IE下不行,它会当成属性之类的来处理
hbgzg3006 2011-02-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ibm_hoojo 的回复:]

引用 3 楼 lxh060204 的回复:

for语句问题:
改为:
for(i=0;i<type.length;i++)
{
alert(type[i]); //奇怪的是长度也会被输出
if (type[i].checked == true) { //这里更是undefined,没有checked属性
search += type[i].id;
}
}……
[/Quote]
为什么呢?for in 就不行?
hoojo 2011-02-21
  • 打赏
  • 举报
回复
[code=XM]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>radio.html </title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function search(){
var context = document.getElementById("context").value;
var type = document.getElementsByName("type");//这里得到的对象貌似没问题
var search = "type=";
for (var i = 0; i < type.length; i++) {
alert(type[i]); //奇怪的是长度也会被输出
if (type[i].checked == true) { //这里更是undefined,没有checked属性
search += type[i].id;
}
}
search += "&context=" + context;
alert(search);
//window.open("http://localhost:8080/search.jsp?" + search );
}
</script>
</head>

<body>
<div class="search fl">
<ul>
<li class="shuru">
<input name="" type="text" id="context" value="" />
</li>
<li>
<input name="type" type="radio" value="" id="title" />
标题
<input name="type" type="radio" id="author" />
作者
<input name="type" type="radio" id="content" />
文章内容
</li>
<li style="text-align: right">
<input type="button" value="go" onclick="search();" />
</li>
</ul>
</div>
</body>
</html>
[/code]
hoojo 2011-02-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lxh060204 的回复:]

for语句问题:
改为:
for(i=0;i<type.length;i++)
{
alert(type[i]); //奇怪的是长度也会被输出
if (type[i].checked == true) { //这里更是undefined,没有checked属性
search += type[i].id;
}
}
[/Quote]
+
用for就没有问题了,你这样for in会输出属性的
[code=HTM]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>radio.html</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function search(){
var context = document.getElementById("context").value;
var type = document.getElementsByName("type");//这里得到的对象貌似没问题
var search = "type=";
for (var i = 0; i < type.length; i++) {
alert(type[i]); //奇怪的是长度也会被输出
if (type[i].checked == true) { //这里更是undefined,没有checked属性
search += type[i].id;
}
}
search += "&context=" + context;
alert(search);
//window.open("http://localhost:8080/search.jsp?" + search );
}
</script>
</head>

<body>
<div class="search fl">
<ul>
<li class="shuru">
<input name="" type="text" id="context" value="" />
</li>
<li>
<input name="type" type="radio" value="" id="title" />
标题
<input name="type" type="radio" id="author" />
作者
<input name="type" type="radio" id="content" />
文章内容
</li>
<li style="text-align: right">
<input type="button" value="go" onclick="search();" />
</li>
</ul>
</div>
</body>
</html>

[/code]
xuminwlt 2011-02-21
  • 打赏
  • 举报
回复
很明显 i 的概念被覆盖了
JJYY0088 2011-02-21
  • 打赏
  • 举报
回复
for语句问题:
改为:
for(i=0;i<type.length;i++)
{
alert(type[i]); //奇怪的是长度也会被输出
if (type[i].checked == true) { //这里更是undefined,没有checked属性
search += type[i].id;
}
}

bennman 2011-02-21
  • 打赏
  • 举报
回复
[code=HTM]
function search(){
var context = document.getElementById("context").value;
var type = document.getElementsByTagName('input');//这里得到的对象貌似没问题
var temp='';
for(i in type){
if(type[i].type=='radio'){
if(type[i].checked==true){
temp=type[i].id;
}
}
}
alert('raido:'+temp+' context:'+context)
}
[/code]
hbgzg3006 2011-02-21
  • 打赏
  • 举报
回复
怎么会没有格式?

[code=JScrip]
function search(){
var context = document.getElementById("context").value;
var type = document.getElementsByName("type");//这里得到的对象貌似没问题
var search = "type=";
for (i in type) {
alert(type[i]); //奇怪的是长度也会被输出
if (type[i].checked == true) { //这里更是undefined,没有checked属性
search += type[i].id;
}
}
search += "&context=" + context;
alert(search);
//window.open("http://localhost:8080/search.jsp?" + search );
}
[/code]

[code=HTM]
<div class="search fl">
<ul>
<li class="shuru"> <input name="" type="text" id ="context" value=""/> </li>
<li> <input name="type" type="radio" value="" id = "title"/> 标题 <input name="type" type="radio" id="author"/> 作者 <input name="type" type="radio" id="content"/> 文章内容 </li>
<li style="text-align:right"> <input name="" type="image" value="" onClick="search();" /> </li>
</ul>
</div>
[/code]
hongmei85 2011-02-21
  • 打赏
  • 举报
回复
	<input type="checkbox" name="type" value="123" checked id="c1">
<input type="checkbox" name="type" value="456" checked id="c2">
<script type="text/javascript">
<!--
function search(){
var type = document.getElementsByName("type");//这里得到的对象貌似没问题

var search = "type=";
for (var i=0;i<type.length;i++) {
if (type[i].checked == true) {
search += type[i].id;
}
}
alert(search);
//window.open("http://localhost:8080/search.jsp?" + search );
}
search();
//-->
</script>
HSBOY86 2011-02-21
  • 打赏
  • 举报
回复
<html>
<head>
<title></title>
</head>
<body>
<input name="type" type="radio" value="" id = "title" checked="true"/> 标题 <input name="type" type="radio" id="author"/> 作者 <input name="type"

type="radio" id="content"/> 文章内容
</body>
<script>
var type = document.getElementsByName("type");
for (var i=0;i<type.length;i++) {
alert(type[i].checked);
}
</script>
</html>
hbgzg3006 2011-02-21
  • 打赏
  • 举报
回复

哪位大牛解释一下为什么?
hongmei85 2011-02-21
  • 打赏
  • 举报
回复
function search(){ 
var context = document.getElementById("context").value;
var type = document.getElementsByName("type");//这里得到的对象貌似没问题
var search = "type=";
for (i in type) {
alert(i); //奇怪的是长度也会被输出
if (i.checked == true) { //这里更是undefined,没有checked属性
search += i.id;
}
}
search += "&context=" + context;
alert(search);
//window.open("http://localhost:8080/search.jsp?" + search );
}

hbgzg3006 2011-02-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dongxinxi 的回复:]

<li> <input name="type" type="radio" value="" id = "title"/>

FF允许用这些特殊名字来命名元素
但IE下不行,它会当成属性之类的来处理
[/Quote]
即使我不用type来命名,也是有上述问题,因此应该不是这个的原因。

87,910

社区成员

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

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