在JavaScript中为ListBox项添加和删除文本框值 [英] Add and Remove text box value to ListBox Items in JavaScript

查看:91
本文介绍了在JavaScript中为ListBox项添加和删除文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Can any one please tell me in ASP.NET How to ADD or REMOVE text box value to List Box item using Add and Remove buttons this should be done using Java script only without server post back.

This is my code, but its not working.

ASPX :-
<input type="button" value="Add Item" onclick="javascript:addValue()" />
<input type="button" value="Delete Item" onclick="javascript:deleteValue()" />
 <asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
<asp:ListBox ID="LstChanelType" runat="server"></asp:ListBox>


Javascript :-
<script type="text/javascript">
var i = 0;
function addValue() {
var v = document.form1.txtChanelType.value;
// get the TextBox Value and assign it into the variable
AddOpt = new Option(v, v);
document.form1.LstChanelType.options[i++] = AddOpt;
return true;
}
function deleteValue() {
var s = 1;
var Index;
if (document.form1.LstChanelType.selectedIndex == -1) {
alert("Please select any item from the ListBox");
return true;
}
while (s > 0) {
Index = document.form1.LstChanelType.selectedIndex;
if (Index >= 0) {
document.form1.LstChanelType.options[Index] = null;
--i;
}
else
s = 0;
}
return true;
}
</script>


Always here i am getting an error message  :-
 Compiler Error Message: CS1026: ) expected
 Line 192: <asp:Button ID="add" Text="Add" CssClass="form_submit" runat="server" onclick="javascript:addValue()" />

Please please help me...

推荐答案

问题已修复



var i = 0;

function addValue(){

// alert(Hi);

var v = document.getElementById(MainContent_txtValue)。value;

// alert(v);

//获取TextBox值并分配入变量

AddOpt =新选项(v,v);

的document.getElementById( MainContent_LstChanelType)选项[I ++] = AddOpt;

//document.form1.LstChanelType.options [i + +] = AddOpt;

返回true;

}



函数deleteValue(){

var s = 1;

var索引;



if(document.getElementById(MainContent_LstChanelType)。selectedIndex == -1)

{

alert(请从ListBox中选择任何项目);

返回true;



}
Issue has been fixed

var i = 0;
function addValue() {
// alert("Hi");
var v = document.getElementById("MainContent_txtValue").value;
// alert(v);
// get the TextBox Value and assign it into the variable
AddOpt = new Option(v, v);
document.getElementById("MainContent_LstChanelType").options[i++] = AddOpt;
//document.form1.LstChanelType.options[i++] = AddOpt;
return true;
}

function deleteValue() {
var s = 1;
var Index;

if(document.getElementById("MainContent_LstChanelType").selectedIndex == -1)
{
alert("Please select any item from the ListBox");
return true;

}


这篇关于在JavaScript中为ListBox项添加和删除文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆