document.getElementByName方法没有
document.getElementsByName得到的是标签的数组
document.getElementId得到的是某一个标签
你这里使用的方法不对,改成getElementId就可以了
代码给你调整完了,你看看<select id="gw1" size="5" style="width: 200px">
<option value="">选择任意职位类别 </option>
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
<option value="4">4 </option>
<option value="5">5 </option>
<option value="6">6 </option>
<option value="7">7 </option>
</select>
<input class="go-wenbenkuang" onclick="SelectOne()" type="button" value="添加>>" id="Input">
<br>
<input class="go-wenbenkuang" onclick="DelOne()" type="button" value=" < <删除" id="Input">
<select id="gw2" size="5" style="width: 200px" id="gw2">
</select>
<input type="hidden" id="hid_JobType" />
<script type="text/javascript">
function SelectOne()
{
var hObjValue = document.getElementById("hid_JobType");
var lst1=window.document.getElementById("gw1");
var lst2=window.document.getElementById("gw2");
var lstindex=lst1.selectedIndex; //当前选择的索引
var length = lst2.options.length; //第二个的总条数
alert(lst2.options.length);
var isExists = false;
if(length >= 5)
{
alert("您只能添加五条");
}
else
{
if(lstindex <=0)
{
alert("您不能添加此条信息");
return;
}
else if(length != null)
{
for(var i=0;i < length; i++)
{
if(lst2.options[i].text == lst1[lstindex].text&&lst2.options[i].value == lst1[lstindex].value)
{
isExists = true;
break;
}
}
}
else
{
return;
}
if (isExists == false)
{
var v = lst1.options[lstindex].value;
var t = lst1.options[lstindex].text;
lst2.options[lst2.options.length] = new Option(t,v,true,true);
hObjValue.value = "";
for( i=0; i <lst2.options.length; i++)
{
hObjValue.value += lst2.options[i].value + ",";
}
lst1.options.selectedIndex=lst1.options.selectedIndex+1;
}
else
{
alert("所选条目已经存在");
return false;
}
}
}
function DelOne()
{
var hObjValue = document.getElementsByName("hid_JobType");
var lst2=window.document.getElementsByName("gw2");
var lstindex=lst2.selectedIndex;
if(lstindex <0)
{
alert("没有可删除项!");
return;
} if(lstindex>=0)
{
var v = lst2.options[lstindex].value+";";
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
lst2.selectedIndex=lstindex-1;
hObjValue.value = "";
for( i=0; i <lst2.options.length; i++)
{
hObjValue.value += lst2.options[i].value + ",";
}
}
}
</script>
document.getElementsByName得到的是标签的数组
document.getElementId得到的是某一个标签
你这里使用的方法不对,改成getElementId就可以了
代码给你调整完了,你看看<select id="gw1" size="5" style="width: 200px">
<option value="">选择任意职位类别 </option>
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
<option value="4">4 </option>
<option value="5">5 </option>
<option value="6">6 </option>
<option value="7">7 </option>
</select>
<input class="go-wenbenkuang" onclick="SelectOne()" type="button" value="添加>>" id="Input">
<br>
<input class="go-wenbenkuang" onclick="DelOne()" type="button" value=" < <删除" id="Input">
<select id="gw2" size="5" style="width: 200px" id="gw2">
</select>
<input type="hidden" id="hid_JobType" />
<script type="text/javascript">
function SelectOne()
{
var hObjValue = document.getElementById("hid_JobType");
var lst1=window.document.getElementById("gw1");
var lst2=window.document.getElementById("gw2");
var lstindex=lst1.selectedIndex; //当前选择的索引
var length = lst2.options.length; //第二个的总条数
alert(lst2.options.length);
var isExists = false;
if(length >= 5)
{
alert("您只能添加五条");
}
else
{
if(lstindex <=0)
{
alert("您不能添加此条信息");
return;
}
else if(length != null)
{
for(var i=0;i < length; i++)
{
if(lst2.options[i].text == lst1[lstindex].text&&lst2.options[i].value == lst1[lstindex].value)
{
isExists = true;
break;
}
}
}
else
{
return;
}
if (isExists == false)
{
var v = lst1.options[lstindex].value;
var t = lst1.options[lstindex].text;
lst2.options[lst2.options.length] = new Option(t,v,true,true);
hObjValue.value = "";
for( i=0; i <lst2.options.length; i++)
{
hObjValue.value += lst2.options[i].value + ",";
}
lst1.options.selectedIndex=lst1.options.selectedIndex+1;
}
else
{
alert("所选条目已经存在");
return false;
}
}
}
function DelOne()
{
var hObjValue = document.getElementsByName("hid_JobType");
var lst2=window.document.getElementsByName("gw2");
var lstindex=lst2.selectedIndex;
if(lstindex <0)
{
alert("没有可删除项!");
return;
} if(lstindex>=0)
{
var v = lst2.options[lstindex].value+";";
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
lst2.selectedIndex=lstindex-1;
hObjValue.value = "";
for( i=0; i <lst2.options.length; i++)
{
hObjValue.value += lst2.options[i].value + ",";
}
}
}
</script>
测试一下看值是多少
1)document.getElementsByName("???")
-->document.getElementsByName("???")[0]
2)
function DelOne()
{
var hObjValue = document.getElementsByName("hid_JobType")[0];
var lst2=window.document.getElementsByName("gw2")[0];
var lstindex=lst2.selectedIndex; //err:options
-->
var lstindex=lst2.options.selectedIndex; <html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head><body>
<select name="gw1" size="5" style="width: 200px">
<option value="">选择任意职位类别 </option>
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
<option value="4">4 </option>
<option value="5">5 </option>
<option value="6">6 </option>
<option value="7">7 </option>
</select>
<input class="go-wenbenkuang" onclick="SelectOne()" type="button" value="添加>>" name="Input"> <br>
<input class="go-wenbenkuang" onclick="DelOne()" type="button" value=" < <删除" name="Input">
<select name="gw2" size="5" style="width: 200px"> </select>
<input type="hidden" name="hid_JobType" >
<script language="javascript">
<!--
function SelectOne() {document.getElementsByName
var hObjValue = document.getElementsByName("hid_JobType")[0];
var lst1=window.document.getElementsByName("gw1")[0];
var lst2=window.document.getElementsByName("gw2")[0];
var lstindex=lst1.selectedIndex; //当前选择的索引
var length = lst2.options.length; //第二个的总条数
alert(lst2.options.length);
var isExists = false;
if(length >= 5)
{
alert("您只能添加五条");
}
else
{
if(lstindex <=0)
{
alert("您不能添加此条信息");
return;
}
else if(length != null)
{
for(var i=0;i < length; i++)
{
if(lst2.options[i].text == lst1[lstindex].text&&lst2.options[i].value == lst1[lstindex].value)
{
isExists = true;
break;
}
}
}
else
{
return;
}
if (isExists == false)
{
var v = lst1.options[lstindex].value;
var t = lst1.options[lstindex].text;
lst2.options[lst2.options.length] = new Option(t,v,true,true);
hObjValue.value = "";
for( i=0; i <lst2.options.length; i++)
{
hObjValue.value += lst2.options[i].value + ",";
}
lst1.options.selectedIndex=lst1.options.selectedIndex+1;
}
else
{
alert("所选条目已经存在");
return false;
}
}
}
function DelOne()
{
var hObjValue = document.getElementsByName("hid_JobType")[0];
var lst2=window.document.getElementsByName("gw2")[0];
////// var lstindex=lst2.selectedIndex; //err:options
var lstindex=lst2.options.selectedIndex;
if(lstindex <0)
{
alert("没有可删除项!");
return;
} if(lstindex>=0) { alert(lstindex)
var v = lst2.options[lstindex].value+";";
lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
lst2.selectedIndex=lstindex-1;
hObjValue.value = "";
for( i=0; i <lst2.options.length; i++)
{
hObjValue.value += lst2.options[i].value + ",";
}
}
}
//-->
</script>
</body></html>