JQuery拖放列表问题
我在我的网站上创建了一个可拖放列表,并且它的拖放功能似乎正在工作。每个列表元素内部都有一个“隐藏”表单字段,其中包含与每个列表元素相关的数据。
现在,如果我根本不拖动列表并提交表单,所有内容都会按预期提交。但是,如果我将一个元素拖动到另一个元素上,则我拖动的元素(或其 DOM 发生移动的任何元素)不会被提交。不太确定这里发生了什么。以下是每个可拖动元素的片段:
<ul class="sortable ui-sortable" id="sortable_buildings">
<li class="ui-state-default" id="1" style=""><input type="hidden" name="order[]" value="128"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Carlu</li>
<li class="ui-state-default" id="1"><input type="hidden" name="order[]" value="158"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>CPR Building</li>
</ul>
和 JQuery 魔法
$(document).ready(function(){
$("#sortable_buildings").sortable();
$('#selected_buildings').change(function(){
$('#sortable_buildings').html('');
var str = "";
$("#selected_buildings option:selected").each(function () {
str += '<li class="ui-state-default" id="1"><input type="hidden" name="order[]" value="' + $(this).attr('value') + '" /><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' + $(this).text() + '</li>'
});
$("#sortable_buildings").append(str);
});
});
编辑:
注意到这对于在表单中创建/修改的所有项目来说都是一个问题。我该如何正确提交表格?
I have created a drag and droppable list on my website, and the drag/drop aspect of it seems to be working. Inside of each list element is a "hidden" form field which contains data assioated with each list element.
Now if I dont drag the list at all, and submit the form, everything submits as expected. However, if I dragged an element over another, the element I dragged(or any element whose DOM got shifted around) does not get submitted. Not really sure whats going on here. Here is a snippet of each draggable element:
<ul class="sortable ui-sortable" id="sortable_buildings">
<li class="ui-state-default" id="1" style=""><input type="hidden" name="order[]" value="128"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Carlu</li>
<li class="ui-state-default" id="1"><input type="hidden" name="order[]" value="158"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>CPR Building</li>
</ul>
And the JQuery magic
$(document).ready(function(){
$("#sortable_buildings").sortable();
$('#selected_buildings').change(function(){
$('#sortable_buildings').html('');
var str = "";
$("#selected_buildings option:selected").each(function () {
str += '<li class="ui-state-default" id="1"><input type="hidden" name="order[]" value="' + $(this).attr('value') + '" /><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' + $(this).text() + '</li>'
});
$("#sortable_buildings").append(str);
});
});
EDIT:
Noticed this is a problem for all items created/modified in the form. How would I properly submit the form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您提交时,在
另外:您不需要创建一个隐藏元素来存储
的值
元素。您应该遵循以下方法:
元素
在这种情况下,代码的第 (1) 点将是:
/* store andappend
;标签 */
/* 检索 */
Inside
<form>
tag when you submit, every hidden element should be serialized and submitted. For this, I think when you are dragging an element it is going beyond the<form>
tag hence not submittedAddition: You need not to create an hidden element to store the value of the
<li>
element. You should follow this approach:<li>
elementIn this case, point (1) of your code will be:
/* store and append <li> tag */
/* retrieve */
问题是基于包含表单元素的表内的表单标签。将表单标签移到表格之外,元素将正确提交。
PS 我只在 FF 中被这个问题困扰,即使表格内有表单标签,IE 也能正常工作。
The problem is based on having the form tag inside the table which contains the form elements. Move the form tags outside of the table and the elements will submit properly.
P.S. I was only plagged by this in FF, IE worked fine even with the form tag inside the table.