JQuery,更新表单和异常(如果更新多次)
我有元素的索引:
<h2>Index</h2>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('button').click(function () {
$.post("Home/Swap", $("#log").serialize(), function (data) {
$("#log").load("Home/Index #log");
});
});
});
</script>
<form name="list" action="<%=Url.Action("Post", "Home")%>" method="post" id="log">
<% foreach (var k in Model) { %>
<input type="checkbox" id="ids" name="ids" value="<%=k.pos%>" /><%= k.pos %>. To jest numer: <%=k.name%><br />
<% } %>
</form>
<button>Swap</button>
和交换方法:
public ActionResult Swap(int[] ids)
{
int pos1=ids[0];
int pos2=ids[1];
Element element1 = (from t in db.Elements
where t.pos == pos1
select t).Single();
Element element2 = (from t in db.Elements
where t.pos == pos2
select t).Single();
element1.pos = pos2;
element2.pos = pos1;
db.SaveChanges();
return Index();
}
第一次交换元素时一切正常。但是当我交换一次,然后尝试交换另外两个时,我得到一个异常:
System.NullReferenceException 是 未由用户代码处理
消息=对象引用未设置为 对象的实例。
(互换方法的例外) 我确定这是 JQuery 问题。我怀疑这个 $("#log").load("Home/Index #log");
行 - 它显示了正确的结果,但如果我尝试做得更多,则无法正常工作然后一次。如何修复它?
编辑:当我刷新页面时,它的工作原理相同 - >首先效果很好,在出现异常后(刷新后第一个触摸的元素被交换)
I've got index of my elements:
<h2>Index</h2>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('button').click(function () {
$.post("Home/Swap", $("#log").serialize(), function (data) {
$("#log").load("Home/Index #log");
});
});
});
</script>
<form name="list" action="<%=Url.Action("Post", "Home")%>" method="post" id="log">
<% foreach (var k in Model) { %>
<input type="checkbox" id="ids" name="ids" value="<%=k.pos%>" /><%= k.pos %>. To jest numer: <%=k.name%><br />
<% } %>
</form>
<button>Swap</button>
and Swap method:
public ActionResult Swap(int[] ids)
{
int pos1=ids[0];
int pos2=ids[1];
Element element1 = (from t in db.Elements
where t.pos == pos1
select t).Single();
Element element2 = (from t in db.Elements
where t.pos == pos2
select t).Single();
element1.pos = pos2;
element2.pos = pos1;
db.SaveChanges();
return Index();
}
Everything works fine at the first swap of elements. But when I swap it once, and then try to swap another two I get an exception:
System.NullReferenceException was
unhandled by user code
Message=Object reference not set to an
instance of an object.
(exception from Swap Method)
It's JQuery problem, Im sure. I susspect this $("#log").load("Home/Index #log");
line - it shows me right result, but doesn't work fine if I try to do it more then once. how to fix it?
edit: when I refresh page it works the same -> first works well, after getting an exception (the first touched elements are swaped after refreshing)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您执行
.load("url 选择器")
时,获取该元素,而不是该元素的内容,因此最终会得到一个嵌套的然后加载 that 元素,因此您替换
When you do
.load("url selector")
it gets that element, not the contents of that element, so you end up with a nested<form>
, so instead create a wrapper and load that wrapper, so you replace the<form>
, like this:Then load that element instead, so you're replacing the
<form>
, like this: