2

我通常是后端开发人员,在前端做的不多,但我有一个主要项目我负责端到端。因此,我正在尝试使用 Twitter Bootstrap 和 jQuery,但在尝试删除我之前使用 jQuery 创建的列表项时发现自己卡住了。我觉得我错过了一些明显的东西,但无法弄清楚它是什么。

这是代码,“从订单中删除”部分是在单击按钮时不做任何事情的部分——似乎什么都没有发生:

编辑:无法让 Jsfiddle 玩得很好,所以我在此处添加了带有代码的 HTML。一切都是内联的(因为这是一个测试文件)或通过 CDN 调用。

<!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8">
            <title>

            </title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta name="description" content="">
            <meta name="author" content="">
            <!-- Le styles -->
            <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
            <!--[if lt IE 9]>
            <script src="js/html5shiv.js"></script>
            <![endif]-->
            <!-- Fav and touch icons -->
            <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png">
            <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png">
            <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png">
            <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png">
            <link rel="shortcut icon" href="ico/favicon.png">
            <link rel="stylesheet" href="js/prettyCheckable/prettyCheckable.css">
            <link type="text/css" rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" />
            <link type="text/css" rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/jquery.selectboxit/3.5.0/jquery.selectBoxIt.css" />

            <script src="js/prettyCheckable/prettyCheckable.js"></script>
            <script>
                    $().ready(function(){

                      $('input.fancyCheckbox').prettyCheckable();

                      //Calls the selectBoxIt method on your HTML select box.
                      $("select").selectBoxIt({
                            theme: "bootstrap",
                            showFirstOption: false
                        });

                       $(".poptip").popover({ trigger: "hover" });
                       $(".inputpopover").popover({ trigger: "focus" });

                       $(".tip").tooltip();


                        // Add To Order Button Click Code
                        $('#idsp-test').keyup(function () {
                            // console.log(this.value);
                            if(parseInt(this.value) > 0) {
                                $('#btn-idsp-add').show();
                            }
                            else {
                                $('#btn-idsp-add').hide();
                            }
                        });

                        $("#btn-idsp-add").on('click', function(){
                            $("#addons_list").append('<li id="idsp-addon-list-item"><i class="icon-ok"></i> ' + $('#idsp-test').val() + ' IDSP <button class="btn btn-mini btn-danger" id="btn-idsp-remove" type="button">Remove From Order</button></li>');
                            // Hide Button After Adding to Sidebar
                            $('#btn-idsp-add').fadeOut('slow');
                        });

                        // REMOVE FROM ORDER BUTTONS
                        $('#btn-idsp-remove').on('click',function(){
                            $('#idsp-test').val('0');
                            $('#idsp-addon-list-item').remove();
                        });

                    });
            </script>
            <style type="text/css">
                .selectboxit-container .selectboxit-options {
                    /* Set's the drop down options width to the same width as the drop down button */
                    width: 210px;
                    /* Set's the max-height property to only show a subset of the drop down items.
                                           If you do not set a max-height property, SelectBoxIt will dynamically
                                           position the dropdown (when opened) to make sure the drop down items are not
                                           displayed outside of the current window viewport.
                                        */
                    max-height: 240px;
                }

                #btn-idsp-add, #btn-county-crim-add {
                    display: none;
                }
            </style>
        </head>
        <body>
            <div class="container-fluid">
                <h1>Package and Addon Selection</h1>
                <div class="container-fluid">
                    <div class="row-fluid">
                        <div class="span4">
                            <form action="index.cfm" method="post" enctype="multipart/form-data">
                                <legend>Package Selection</legend>
                                <div class="control-group">
                                    <div class="controls">
                                        <select id="package_select" name="package_select" class="package_selection">
                                            <option>Basic Packages</option>
                                            <option data-selectedtext = "Standard SmartPak Selected" data-content="Standard SmartPak" value="standard">Standard SmartPak</option>
                                            <option data-selectedtext = "Advanced SmartPak Selected" data-content="Advanced SmartPak" value="advanced">Advanced SmartPak</option>
                                        </select>
                                    </div>
                                <div class="alert alert-success" style="margin-top: 6px; box-shadow: 0 0 2px">
                                <h5>This Package Includes:</h5>
                                    <ul class="unstyled">
                                        <li><i class="icon-ok"></i> One</li>
                                        <li><i class="icon-ok"></i> Two</li>
                                        <li><i class="icon-ok"></i> Three</li>
                                    </ul>
                                </div>
                                <hr>
                                <div id="addons_list_box" class="alert alert-info" style="box-shadow: 0 0 2px">
                                <h5>Additional Addon Items:</h5>
                                    <ul class="unstyled" id="addons_list"></ul>
                                </div>
                                </div>
                        </div>
                        <div class="span8">
                            <fieldset>
                            <!-- Form Name -->
                            <legend>Order Add Ons</legend>
                            <!-- Multiple Checkboxes -->
                            <div class="control-group">
                                <div class="controls controls-row">
                                    <div class="input-wrapper form-inline">
                                        <label for="idsp-test">IDSP
                                        <input type="text" class="mini inline inputpopover" title="Additional IDSPs" data-content="Enter a number from 1 to 10 to add to your order.  Set to zero and click the button to remove or change." data-placement="top" id="idsp-test" name="idsp-test" placeholder="Add a Quantity from 1 to 10." value="" autocomplete="off" />
                                        <button class="btn btn-success" type="button" id="btn-idsp-add"><i class="icon-plus icon-white"></i> Add/Update Order</button>
                                        </label>
                                    </div>
                                </div>
                                <div class="controls controls-row">
                                    <div class="input-wrapper form-inline">
                                        <label for="county-crim-test">County Criminal Search
                                        <input type="text" class="mini inline inputpopover" title="Additional County Criminal Searches" data-content="Enter a number from 1 to 10 to add to your order.  Set to zero and click the button to remove or change." data-placement="top" id="county-crim-test" name="county-crim-test" placeholder="Add a Quantity from 1 to 10." value="" autocomplete="off" />
                                        <button class="btn btn-success" type="button" id="btn-county-crim-add"><i class="icon-plus icon-white"></i> Add/Update Order</button>
                                        </label>
                                    </div>
                                </div>
                                <!-- Button -->
                                <div class="control-group">
                                    <label class="control-label"></label>
                                    <div class="controls">
                                        <input name="submit" type="submit" class="btn btn-primary" id="submit" title="Submit the Form" value="Place My Order" data-toggle="tooltip" data-content="Submit The Form" />
                                    </div>
                                </div>
                            </div>
                            </fieldset>
                    </form>
                </div>
        </div>
    </div>
    </div>
    <!-- /container -->
    <!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.selectboxit/3.5.0/jquery.selectBoxIt.min.js"></script>
    <script src="js/prettyCheckable/prettyCheckable.js"></script>
    </body>
    </html>

有人可以告诉我我做错了什么吗?

谢谢!

4

1 回答 1

1

即使它有点违背“.on”的观点,尝试将点击事件更改为:

// REMOVE FROM ORDER BUTTONS
$(document).on('click', '#btn-idsp-remove', function(){
    $('#idsp-test').val('0');
    $('ul').find('#idsp-addon-list-item').remove();  //assumes you are using "ul" for your list and not "ol"
});

和一个jsFiddle一起去。

于 2013-05-21T03:17:24.633 回答