简体   繁体   English

使用jQuery使将来的列表项可排序

[英]Making future list items sortable using jQuery

I have an input box for users to type in an item and then click the submit button or enter key and it is added to an unordered list with an id of sortable. 我有一个供用户输入项目的输入框,然后单击提交按钮或输入键,它被添加到ID为可排序的无序列表中。 I would like to make this user generated unordered list sortable using jQuery but I can't get it to work. 我想使用jQuery使此用户生成的无序列表可排序,但是我无法使其正常工作。 I believe it involves the use of the on method but my attempts thus far have been completely unsuccessful. 我相信它涉及on方法的使用,但是到目前为止,我的尝试完全没有成功。

Here is the link to the jsfiddle containing the project. 是包含项目的jsfiddle的链接。

Also, here is what I am linking to in the head of the html file: 另外,这是我在html文件的开头链接的内容:

<link rel='stylesheet' type='text/css' href='stylesheet.css'/>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="script.js"></script>

Edit: On jsfiddle the list items are sortable but the enter key can't be used to submit and [object Object] is outputted rather than the input text. 编辑:在jsfiddle上,列表项是可排序的,但是enter键不能用于提交,并且[object Object]而不是输入文本被输出。 When opening the file in browser (chrome) it still is not sortable but the added list items do have the proper outputs. 在浏览器(chrome)中打开文件时,它仍然无法排序,但是添加的列表项确实具有正确的输出。

This would be better if you do something like this 如果您做这样的事情会更好

var changeText2 = function (){

    var listWish = $("#listWish").val();
    var node = $("#sortable").append("<li>")
    $("#sortable li:last-child").text(listWish);
    $("#sortable li").sort(naturalsort).appendTo("#sortable");

}

On JQuery .append() append a child node to the selection and .appendTo() append the selection contains, as child node, to an specific selection. 在JQuery上, .append()将子节点附加到选择中, .appendTo()将选择物包含作为特定子选择的子节点。

You can pass a function to the .sort() to specify how to sort the elements too. 您可以将函数传递给.sort()来指定如何对元素进行排序。

Check my JSFiddle here 在这里检查我的JSFiddle

You would just need to write $('#listWish').val() instead of $('#listWish') . 您只需要编写$('#listWish').val()而不是$('#listWish')

$("#submitButton").on("click", function(){
   $("#sortable").append('<li>' + $('#listWish').val() + '</li>').sortable("refresh");
});

Notice that I'm calling 'refresh' method after adding new item istead of reinializing whole list. 请注意,在添加新项之后,而不是重新初始化整个列表,我正在调用“ refresh”方法。 For more information read here . 欲了解更多信息, 请点击这里

UPDATE: It could be the case when you call $("#sortable").sortable(); 更新:当您调用$("#sortable").sortable();时可能是这种情况$("#sortable").sortable(); before your markup has been loaded. 在标记加载之前。 Try to make it this way: 尝试以这种方式进行:

$(function(){
   $("#sortable").sortable();
})

All code inside $(function(){ }) wrapper will be called after all document is loaded. 装入所有文档后,将调用$(function(){ })包装器中的所有代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM