读书人

50分请看下面的代码如何添加不近去

发布时间: 2012-03-16 16:34:56 作者: rapoo

50分求助,请看下面的代码,怎么添加不近去啊?很难的!!!!!!!!!!!!!!!!!!!!!
我的意思是这样的。选中select中的任何一条或多条数据,然后单击 "添加 "按纽,然后把数据添加到文本框中去,数据之间用 ", "号隔开,其中数据不能重复,也就是说,假设把aa添近去之后,再添aa,就田不近去了。这个功能我已经实现了。现在的问题是。假设里面有了aabb,这时aa应该能添进去。可是我的不能实现。我的源代码如下:
<script type= "text/javascript " language= "javascript ">
function acceptSubmit()
{
var selectUserName = document.listForm.selectUserName;
var selectText;
with (document.listForm)
{
var sss=toUser.value;
var arr=toUser.value.split( ", ");
for (var i = 0; i < selectUserName.length; i++)
{
if (selectUserName.options[i].selected)
{
selectText = selectUserName[i].text;
var arr=toUser.value.split( ", ");
if (toUser.value.indexOf(selectText) == -1)
{
toUser.value += ", " + selectText;
}
}
}
if (toUser.value.indexOf( ', ') == 0)
{
toUser.value =toUser.value.substring(1, toUser.value.length);
}

}
}
</script>
</head>
<body>
<form id= "listForm " name= "listForm " method= "post " action= " ">
<input type=text name= "toUser " value= " " style= "width:2000 ">
<table border= "0 " cellspacing= "0 " cellpadding= "0 ">
<tbody>
<tr>
<td width= "200 " align= "center ">
<select id= "selectUserName " name= 'selectUserName " ' multiple size= "10 "style= "width: 200px;height:260px ">
<option value= "100 "> aa </option>
<option value= "100 "> bb </option>


<option value= "101 "> aabb </option>
<option value= "102 "> cc </option>
<option value= "103 "> dd </option>
<option value= "104 "> ee </option>
<option value= "105 "> ff </option>
<option value= "106 "> gg </option>
</select>
</td>
</tr>
<tr>
<td>    </td>
<td>    </td>
<tr>
<tr>
<td>
   <input type= "button " name= "btnAccept " style= "color:blue " value= "添加 "
onclick= "acceptSubmit() ">       
<input type= "button " name= "btnCancel " style= "color:blue " value= "Cancel " onclick= "window.close() ">
</td>
</tr>
</tbody>
</table>
</form>
</body>




[解决办法]
<script type= "text/javascript " language= "javascript ">
function acceptSubmit()
{
var selectUserName = document.listForm.selectUserName;
var selectText;
with (document.listForm)


{
var sss=toUser.value;
var arr=toUser.value.split( ", ");
for (var i = 0; i < selectUserName.length; i++)
{
if (selectUserName.options[i].selected)
{
selectText = selectUserName[i].text;
var arr=toUser.value.split( ", ");
//if (toUser.value.indexOf(selectText) == -1)
//{
//toUser.value += ", " + selectText;
//}
//***************************************************
var flag= "false "
for(var aaa=0;aaa <arr.length;aaa++){
if (arr[aaa]==selectText)
{
flag= "true "
//toUser.value += ", " + selectText;
}
}
if(flag== "false "){
toUser.value += ", " + selectText;
}
//***************************************************
}
}
if (toUser.value.indexOf( ', ') == 0)
{
toUser.value =toUser.value.substring(1, toUser.value.length);
}

}
}
</script>
</head>
<body>
<form id= "listForm " name= "listForm " method= "post " action= " ">
<input type=text name= "toUser " value= " " style= "width:2000 ">
<table border= "0 " cellspacing= "0 " cellpadding= "0 ">
<tbody>
<tr>
<td width= "200 " align= "center ">
<select id= "selectUserName " name= 'selectUserName " ' multiple size= "10 "style= "width: 200px;height:260px ">
<option value= "100 "> aa </option>
<option value= "100 "> bb </option>
<option value= "101 "> aabb </option>
<option value= "102 "> cc </option>
<option value= "103 "> dd </option>
<option value= "104 "> ee </option>
<option value= "105 "> ff </option>
<option value= "106 "> gg </option>
</select>
</td>
</tr>
<tr>
<td>    </td>
<td>    </td>
<tr>
<tr>
<td>
   <input type= "button " name= "btnAccept " style= "color:blue " value= "添加 "
onclick= "acceptSubmit() ">       
<input type= "button " name= "btnCancel " style= "color:blue " value= "Cancel " onclick= "window.close() ">
</td>
</tr>
</tbody>
</table>
</form>
</body>
[解决办法]
用我给你的代码把你的替换掉就行了( <sciprt … </script> 之间的东西 其他不要动 )
<script type= "text/javascript " language= "javascript ">
function acceptSubmit()
{
var selectUserName = document.listForm.selectUserName;
var selectText;
with (document.listForm)
{
var sss=toUser.value;
var arr=toUser.value.split( ", ");
for (var i = 0; i < selectUserName.length; i++)
{
if (selectUserName.options[i].selected)
{
selectText = selectUserName[i].text;
var arr=toUser.value.split( ", ");
for(var n=0;n <arr.length;n++){
if(arr[n]==selectText){
return;
}
}
toUser.value += ", " + selectText;


}
}
if (toUser.value.indexOf( ', ') == 0)
{
toUser.value =toUser.value.substring(1, toUser.value.length);
}

}
}
</script>

读书人网 >JavaScript

热点推荐