要动态添加input
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var spotMax = 10;
if($('div.spot').size() >= spotMax) {$(obj).hide();}
$("input#add").click(function(){addSpot(this, spotMax);
});
});
function addSpot(obj, sm) { $('div#spots').append(
'<div class="spot">' +
'<input type="text" name="spot_title"/> ' +
'<input type="text" name="spot_addr" /> ' +
'<input type="text" name="spot_url" /> ' +
'<input type="button" class="remove" value="Delete" /></div>')
.find("input.remove").click(function(){
$(this).parent().remove();
$('input#add').show();
});
if($('div.spot').size() >= sm) {$(obj).hide();}
};
</script>
</head>
<body>
<form method="post" name="asdf" id="asdf" runat="server">
<div id="spots">
<input type="button" id="add" name="add" value="add" /><br />
</div>
<input type="button" name="Submit" value="Submit" id="send" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Button" />
</form> 要前台判断值,后台取值保存到数据库,如何前后台取值?这个有动态添加也有动态删除功能。
<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var spotMax = 10;
if($('div.spot').size() >= spotMax) {$(obj).hide();}
$("input#add").click(function(){addSpot(this, spotMax);
});
});
function addSpot(obj, sm) { $('div#spots').append(
'<div class="spot">' +
'<input type="text" name="spot_title"/> ' +
'<input type="text" name="spot_addr" /> ' +
'<input type="text" name="spot_url" /> ' +
'<input type="button" class="remove" value="Delete" /></div>')
.find("input.remove").click(function(){
$(this).parent().remove();
$('input#add').show();
});
if($('div.spot').size() >= sm) {$(obj).hide();}
};
</script>
</head>
<body>
<form method="post" name="asdf" id="asdf" runat="server">
<div id="spots">
<input type="button" id="add" name="add" value="add" /><br />
</div>
<input type="button" name="Submit" value="Submit" id="send" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Text="Button" />
</form> 要前台判断值,后台取值保存到数据库,如何前后台取值?这个有动态添加也有动态删除功能。
前台:document.getElementById("id").value
name="test"+i可以这样命名
我建议的做法是
首先,未必一定要使用ID,既然你用的是jQuery,可以考虑使用each来取值。删除可以使用this绑定事件。。
其次,后台取值通常可以选择使用ajax。但是我曾经也这样做过,就是先是使用js把值通过一个规定的数据格式比如"first|second|third"放到一个固定的隐藏控件里去,后台直接取到值之后对比做相应的处理。
另外一种就是用服务器隐藏控件(如<asp:HiddenFiled runat="server" id="hf" />),客户端可以在你的按钮上加上客户端onclick和服务器onclick
如<asp:Button ID="Button1" runat="server" OnClientClick="document.getElementById('<%=hf.ClientID%>').value='123'" OnClick="Button1_Click1" Text="Button" />
然后就在后台事件Button1_Click1 里通过hf.Value就可以取得值 不过这种方式会刷新页面