简体   繁体   English

如何使jquery.dynamiclist与更高版本的jquery一起运行?

[英]How to make jquery.dynamiclist run with newer version of jquery?

I would like to use jquery.dynamiclist library in my procject. 我想在我的程序中使用jquery.dynamiclist库。 In the demo page that's running smoothly there is a jquery library in version 1.8.2. 在顺利运行的演示页面中,有一个1.8.2版的jquery库。 In my project I use version 1.11.1 and this makes dynamiclist plugin doesn't work. 在我的项目中,我使用1.11.1版,这使dynamiclist插件无法正常工作。 With newer version when I process form I get data with names of inputs but without values (I get 'undefined'). 在处理表单时,使用较新版本时,我得到的数据带有输入名称,但没有值(我得到“未定义”)。

Here is the code from demo. 这是演示中的代码。 What I have to change to make it run with newer version of jquery? 我需要更改使其与更高版本的jquery一起运行?

    <form class="form-horizontal">
            <h2>Example 1: Basic List</h2>
            <div class="control-group">
                <label class="control-label">Party</label>
                <div class="controls">
                    <input name="partyName" type="text" placeholder="Party Name">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Guest List</label>
                <div id="example1" class="controls">
                    <div class="list-item">
                        <input name="guestList[0].name" type="text" placeholder="Guest Name">
                        <a href="#" class="list-remove btn btn-small"><i class="icon-minus"></i> Remove Guest</a>
                    </div>
                    <a href="#" class="list-add btn btn-small"><i class="icon-plus"></i> Add Guest</a>
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <input type="submit" class="btn btn-primary btn-large" value="Process Example 1"/>
                </div>
            </div>
        </form>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

   <script src="https://github.com/ikelin/jquery.dynamiclist/blob/master/jquery.dynamiclist.min.js"></script>
        <script>
            (function($) {
                $(document).ready(function() {
                    $("#example1").dynamiclist();

                    // display form submit data
                    $("form").submit(function(event) {
                        event.preventDefault();
                        var data = "";
                        $(this).find("input, select").each(function() {
                            var element = $(this);
                            if (element.attr("type") != "submit") {
                                data += element.attr("name");
                                data += "="
                                data += element.attr("value");
                                data += "; "
                            }
                        });
                        alert(data);
                        location.reload(true);
                    });
                });
            })(jQuery);
        </script>

Simply change element.attr("value"); 只需更改element.attr("value"); to element.val(); element.val(); .

Sorry I had to include the plugin code manually because apparently you cannot include the src directly from github. 抱歉,我必须手动包含插件代码,因为显然您不能直接从github包含src。 Also commented the reload for the snippet. 还评论了代码段的重新加载。

  <form class="form-horizontal"> <h2>Example 1: Basic List</h2> <div class="control-group"> <label class="control-label">Party</label> <div class="controls"> <input name="partyName" type="text" placeholder="Party Name"> </div> </div> <div class="control-group"> <label class="control-label">Guest List</label> <div id="example1" class="controls"> <div class="list-item"> <input name="guestList[0].name" type="text" placeholder="Guest Name"> <a href="#" class="list-remove btn btn-small"><i class="icon-minus"></i> Remove Guest</a> </div> <a href="#" class="list-add btn btn-small"><i class="icon-plus"></i> Add Guest</a> </div> </div> <div class="control-group"> <div class="controls"> <input type="submit" class="btn btn-primary btn-large" value="Process Example 1"/> </div> </div> </form> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> /* jQuery Dynamic List v 2.0.1 / Copyright 2012 Ike Lin / http://www.apache.org/licenses/LICENSE-2.0.txt */ (function(a){a.fn.dynamiclist=function(d){if(this.length>1){this.each(function(){a(this).dynamiclist(d)});return this}var g=a.extend({itemClass:"list-item",addClass:"list-add",removeClass:"list-remove",minSize:1,maxSize:10,withEvents:false,addCallbackFn:null,removeCallbackFn:null},d);var f=function(o,n,j){var m=o.find("."+j.itemClass).length;if(m<j.maxSize){var l=o.find("."+j.itemClass+":first").clone(j.withEvents);l.find("."+j.removeClass).show().click(function(p){e(o,a(this),p,j)});b(l,m);i(l);var k=o.find("."+j.itemClass+":last");k.after(l);if(j.addCallbackFn!=null){j.addCallbackFn(l)}}if(n!=null){n.preventDefault()}};var e=function(o,k,n,j){var m=o.find("."+j.itemClass).length;var l=k.parents("."+j.itemClass+":first");if(m==j.minSize){i(l)}else{l.remove()}c(o,j);if(j.removeCallbackFn!=null){j.removeCallbackFn(l)}n.preventDefault()};var b=function(j,k){j.find("label, input, select, textarea").each(function(){var m=["class","name","id","for"];for(var n=0;n<m.length;n++){var l=a(this).attr(m[n]);if(l){l=l.replace(/\\d+\\./,k+".");l=l.replace(/\\[\\d+\\]\\./,"["+k+"].")}a(this).attr(m[n],l)}})};var c=function(k,j){k.find("."+j.itemClass).each(function(){var l=k.find("."+j.itemClass).index(this);b(a(this),l)})};var i=function(j){j.find("input[type=text], textarea").val("");j.find("input[type=radio]").attr({checked:false});j.find("input[type=checkbox]").attr({checked:false})};var h=function(k){k.find("."+g.itemClass+":first ."+g.removeClass).hide();var j=k.find("."+g.itemClass).length;while(g.minSize>j){f(k,null,g);j++}k.find("."+g.addClass).click(function(l){f(k,l,g)});k.find("."+g.removeClass).click(function(l){e(k,a(this),l,g)});return k};return h(this)}})(jQuery); </script> <script> (function($) { $(document).ready(function() { $("#example1").dynamiclist(); // display form submit data $("form").submit(function(event) { event.preventDefault(); var data = ""; $(this).find("input, select").each(function() { var element = $(this); if (element.attr("type") != "submit") { data += element.attr("name"); data += "=" data += element.val(); data += "; " } }); alert(data); //location.reload(true); }); }); })(jQuery); </script> 

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

相关问题 检查jQuery版本并加载更新版本 - Check jQuery version and load a newer version 较新的jQuery版本导致错误的AJAX回调 - Newer jQuery version causes a wrong AJAX callback 如何修复javascript在新的jquery库上不起作用,而在旧版本的jquery库上起作用? - How to fix javascript not working on newer jquery library but working on older version jquery library? 在用户脚本/greasemonkey 中,当网站已经使用旧版本时,如何使用更新版本的 jQuery? - In a userscript/greasemonkey, how do you use a newer version of jQuery when the website already uses an older version? 从不同范围访问较新版本的变量(JavaScript / jQuery) - Accessing Newer Version of Variable from Different Scope (JavaScript / jQuery) 需要帮助以使以前可用的jQuery脚本在较新版本上运行 - Need help getting a previously working jQuery script to work on newer version 如何使这个 jQuery 代码在 3.2.1 版上工作 - How to make this jQuery code work on version 3.2.1 jQuery旧版本和较新版本中的select值(禁用的选项)中的差异 - Discrepancies in select value(disabled option) in jQuery old and newer version 如何让jquery动画函数并行运行? - How to make jquery animation functions run in parallel? jQuery:如何制作新的“按钮”来运行这个 function - jQuery: How to make new “button” to run this function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM