3

バックグラウンド

現在のプロジェクトでは、JQueryUIを使用して製品と買い物かごを実装しました。これはJQueryUIを実装する最初の試みですが、JQueryAPIで十分です。ショップは、c#を使用して.Netで駆動されます。私は、Webの隅々まで検索し、JQuery APIのすべてのページをめくった開発で、このレンガにぶつかりました。コードのロジックの何が問題なのか理解できません。

私の問題

一部の製品には、買い物かごに送る前に選択する必要のある追加のオプションがあります。十分に公平なので、ユーザーから必要な情報を取得するためのダイアログフォーム(JQuery Dialod Modal)を実装し、各製品内のテーブルを更新しました。プログラムのすべては、ユーザー入力で関連製品を更新するまで機能します。製品を更新するたびに、最初の製品のみが詳細を取得します。私の問題を皆さんに明確に説明したことを願っています...同じ問題をシミュレートする基本的なフィドルも以下に含めました。私

情報アイコンをクリックして[ハンドル設定の追加]をクリックし、情報を入力してから最初の製品をもう一度確認すると、意味がわかります。すべてが最初の製品にのみ印刷されます。各製品に書き込む方法が必要です。

製品は、HTMLの順序付けされていないリストを使用して構築されています。したがって、サンプル製品は次のようになります。

HTML

    <li class="ui-widget-content ui-corner-tr" id="100400">
    <h5 class="ui-widget-header">Option 1</h5>

            <font class="repairDetails" alt="Repair Details" style="">DETAIL
            <font style="color:Red; text-decoration:blink; font-size:1em; bottom:10;">Please view product details for <u>handle preferences</u> before sellecting this option.</font>
            </font><br />
            <label class="price">£00.00</label>

            <a class="ui-icon ui-icon-info dialog_but" title="View Product Detail" href="#" style="">View Product Detail</a>
              <div class="dialog_content" title="" style="">

                    <table  style="border:none !important;">
                    <thead>
                    </thead>
                    <tr>
                    <td style="border:none !important; width:15px; height:13px !important;"><span class="ui-icon ui-icon-circle-plus"></span></td>
                    <td style="text-align:left;">
                    DETAIL
                    </td>
                    </tr>

                    <tr>
                    <td style="border:none !important; width:15px; height:13px !important;"><span class="ui-icon ui-icon-circle-plus"></span></td>
                    <td style="text-align:left;">
                    DETAIL
                    </td>
                    </tr>

                    <tr>
                    <td style="border:none !important; width:15px; height:13px !important;"><span class="ui-icon ui-icon-circle-plus"></span></td>
                    <td style="text-align:left;">
                    DETAIL
                    </td>
                    </tr>

                    <tr>
                    <td style="border:none !important; width:15px; height:13px !important;"><span class="ui-icon ui-icon-circle-plus"></span></td>
                    <td style="text-align:left;">
                     DETAIL
                    </td>
                    </tr>

                    <table id="hDetails" class="ui-widget ui-widget-content">
                    <button id="handle-pre" class="handle-pre">Add Handle Preferance</button>

                    <thead> 
                        <tr class="ui-widget-header ">
                        Handle Preferences
                            <th>Handle Shape (Pro, Oval, Round)</th><br />
                            <th>Size - SSH/SH/LH/H etc.</th>
                            <th>Specify handle type if sellected <br />Value package re-handle</th>
                        </tr>
                    </thead>
                    <tbody id="addHere" class="addHere">

                    </tbody>
                    </table>


                    </table>

                </div>
            <a href="" title="Add to chart" class="ui-icon ui-icon-cart chart_but">Add to chart</a>

    <div style="display:none;">

    </div>

</li>

JQUERY

     //Modal window to ask and submit additional user details
        $(function () {
            var hShape = $("#hShape"),
                hSize = $("#hSize"),
                hType = $("#hType"),

        allFields = $([]).add(hShape).add(hSize).add(hType),
        tips = $(".validateTips");

            function updateTips(t) {
                tips
            .text(t)
            .addClass("ui-state-highlight");
                setTimeout(function () {
                    tips.removeClass("ui-state-highlight", 1500);
                }, 500);
            }


            function checkLength(o, n, min) {

                //check handle shape. Make sure the user only inputs relevent options
                if (n == "Handle Shape") {
                    if (o.val() != "Pro" && o.val() != "Oval" && o.val() != "Round") {
                        o.addClass("ui-state-error");
                        updateTips(n + " can only have values of 'Pro', 'Oval' and 'Round'  " + ".");
                        return false;
                    }
                }
                //check lenght 
                if (o.val().length < min) {
                    o.addClass("ui-state-error");
                    updateTips(n + " cannot be blank" + ".");
                    return false;
                } else {
                    return true;
                }
            }


            $("#dialog-form").dialog({
                autoOpen: false,
                height: 400,
                width: 400,
                modal: true,
                show: 'fade',
                hide: 'fade',
                buttons: {
                    "Add Details": function () {
                        var bValid = true;
                        allFields.removeClass("ui-state-error");

                        bValid = bValid && checkLength(hShape, "Handle Shape", 3);
                        bValid = bValid && checkLength(hSize, "Handle Size", 1);
                        bValid = bValid && checkLength(hType, "Handle Type", 3);


                        if (bValid) {
                           $('#addHere').each(function () {
                            $(this).append("<tr>" +
                                "<td>" + hShape.val() + "</td>" +
                                "<td>" + hSize.val() + "</td>" +
                                "<td>" + hType.val() + "</td>" +
                                "</tr>")
                       });
                            $(this).dialog("close");
                        }
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                },
                close: function () {
                    allFields.val("").removeClass("ui-state-error");
                }
            });

             $("#handle-pre").live("click", function () {

                $("#dialog-form").dialog("open");
            });
        });

特に、関連するテーブルを更新しようとしているコードでは、

if (bValid) {
   $('#addHere').each(function () {
      $(this).append("<tr>" +
        "<td>" + hShape.val() + "</td>" +
        "<td>" + hSize.val() + "</td>" +
        "<td>" + hType.val() + "</td>" +
        "</tr>")
    });
 };

私の説明が不十分な場合は、フィドルをご覧ください。これがフィドルです...

4

1 に答える 1

0

jQueryのIDセレクターが指定されたIDに一致する最初の要素のみを見つけるIDで要素を選択しています。あなたの場合、要素を探すコンテキストを指定できます

// find the main dialog element
var $el = $(this).closest('.ui-dialog');

if (bValid) {
     // set the context to the visible div in the background - which is a previous sibling to current dialog
    $('#addHere', $el.prevAll('.ui-dialog:visible')).append("<tr>" + "<td>" + hShape.val() + "</td>" + "<td>" + hSize.val() + "</td>" + "<td>" + hType.val() + "</td>" + "</tr>")
}

http://jsfiddle.net/4R9B6/

using$('#addHere', $el.prevAll('.ui-dialog:visible'))は、addHere 要素を検索するコンテキストを設定しています。dom を検査した後、以前に表示された ui-dialog はどれですか

またID、 は一意である必要があります。そのため、クラスを使用する場合に必要なソリューションは同じですが、クラスを使用する方がおそらく良いでしょう。

于 2012-12-18T16:32:19.347 に答える