2013-01-10 10 views
0

背景:如何查找包含模態數據的div?

在產品被添加到結帳圖表之前,我需要激活一個模式窗口,它將具有自定義選項。只有在此事件發生後,該項目才應添加到圖表中。如果我使用其他語言(c#),則可以實現此功能,但我想了解更多JQuery和JQuery UI。

問題:

我已經試過各種方法與數據分配容器,但到目前爲止我所有的方法都失敗了。所以我簡化了這個程序,因爲HTML端大約有2000行代碼,我不喜歡讓你們擔心。請忽略像額外的標記標籤和結束標籤丟失的任何愚蠢的錯誤,因爲我試圖從原始來源縮短問題,並可能錯過了一些東西。

部分HTML:

<html> 
    <head></head>  
    <body> 
    <div class="ui-widget ui-helper-clearfix" style="height:100%; float:left !important; border:2px solid Black;"> 

    <center> 
    <div class="optionsHeader" style="">RECOMMENDED VALUE PACKAGES</div></center><br /> 
    <ul id="options" class="options ui-helper-reset ui-helper-clearfix"> 
    <li class="ui-widget-content ui-corner-tr" id="100400" title="Please view product details for handle preferences before sellecting this option."> 
    <h5 class="ui-widget-header">Option 1</h5> 
      <font class="repairDetails" alt="Repair Details" style="">REHANDLE<br />FULL BLADE/TOE<br />REPAIR/REFURB</font> 
      <font style="color:Red; text-decoration:blink; font-size:0.7em; bottom:10;">Please view product details for <u>handle preferences</u> before sellecting this option. 
      </font><br /> 
     <label class="price">£49.99</label> 
      <a class="ui-icon ui-icon-wrench dialog_but" title="View Product Detail" href="#" style="">View Product Detail</a> 
       <div class="dialog_content" title="" style=""> 

       <div id="products"> 
       <h1 class="ui-widget-header">Avaliable Bundle Options</h1> 
       <font>You can customise this option below</font> 

       <div id="pOptions"> 
        <h2><a href="#">Handle</a></h2> 
        <div> 
         <ul> 

         </ul> 
        </div> 
        <h2><a href="#">Blade</a></h2> 
        <div> 
         <ul> 

         </ul> 
        </div> 
        <h2><a href="#">Grips</a></h2> 
        <div> 
         <ul> 

         </ul> 
        </div> 
       </div> 
      </div> 

<div id="cart"> 
    <h1 class="ui-widget-header">Sellected Options</h1> 
    <div class="ui-widget-content"> 
     <ol> 
      <li class="placeholder"><i>Drag & Drop</i> your bat repair here</li> 
     </ol> 
    </div> 
</div> 

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

</li> 


</ul> 

    </div> 

    <div id="chart" class="ui-widget-content ui-state-default"> 
    <h4 class="ui-widget-header"><span class="ui-icon ui-icon-cart"></span>Repair Options  You Have Chosen</h4> 
    </div> 
    </body> 
    </html> 

部分JQuery的:

$(function() { 
// there's the options and the chart 
var $options = $("#options"), 
    $chart = $("#chart"); 


//Add to chart 
var recycle_icon = "<a href='' title='Remove this option' class='ui-icon ui-icon-arrowreturnthick-1-w'>Remove Option</a>"; 

function addToChart($item) { 
// He is where i need to prompt the modal than continue as below; 



    $item.fadeOut(function() { 
     var $list = $("ul", $chart).length ? $("ul", $chart) : $("<ul class='options  <ui-helper-> </ui-helper->reset'/>").appendTo($chart); 

     $item.find("a.ui-icon-cart").remove(); 
     $item.append(recycle_icon).appendTo($list).fadeIn(function() { 
      $item.animate({ 
       width: "100%px", 
       height: "100%px" 
      }) //Box 
      .find("font").animate({}); //Font 
     }); 
    }); 
} 

// revert frm chart function 
var chart_icon = "<a href='' title='Add this option' class='ui-icon ui-icon-cart'>Add Option</a>"; 

function revertFrmChart($item) { 
    $item.fadeOut(function() { 
     $item.find("a.ui-icon-arrowreturnthick-1-w").remove().end().css("width", "100%px").append(chart_icon).find("font").css("height", "100%px").end().appendTo($options).fadeIn(); 
    }); 
} 

// resolve the icons behavior with event delegation 
$("ul.options > li").click(function(event) { 
    var $item = $(this), 
     $target = $(event.target); 

    if ($target.is("a.ui-icon-cart")) { 
     addToChart($item); 
    } else if ($target.is("a.ui-icon-arrowreturnthick-1-w")) { 
     revertFrmChart($item); 
    } 

    return false; 
    }); 
    }); 


    //do when the dom is ready 
    jQuery(document).ready(function($) { 
//loop through every button anchor element 
$('.dialog_but').each(function() { 
    //create a local scope of a dialog variable to attach 
    var $dialog; 

    //create the dialog for the div.dialog_content standing next to the anchor element 
    //I set the autoOpen false so that it can be reusable later 
    //also I set the modal = true to appear the dialog as a modal prompt 
    $dialog = $(this).next('div.dialog_content').dialog({ 
     title: 'Product Detail', 
     modal: true, 
     autoOpen: false, 
     width: 600, 
     show: 'fade', 
     hide: 'fade' 
    }); 

    //Swapped this with the inline version 
    //add a wee bit of button thing 
    //     $(this).button({ 
    //      icons: { 
    //       primary: 'ui-icon-info' 
    //      } 
    //     }); 
    //now attach the open event of the dialog to the anchor element 
    $(this).click(function(e) { 
     //prevent the anchor element to go to the hyperlinked page 
     e.preventDefault(); 

     //open the dialog 
     $dialog.dialog('open'); 
    }); //done 
}); 
    }); 




    //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"); 

$("#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); 
      // find relevant parent element 
      var $el = $(this).closest('.ui-dialog'); 

      if (bValid) { 
       // set the context to the visible div in the background - 
       $('#addHere', $el.prevAll('.ui-dialog:visible')).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"); 
}); 
    }); 

的jsfiddle:

如果你點擊扳手圖標,你會採用模態提示。這是用戶點擊添加圖表時需要觸發的模式。

LINK

+0

你正在尋找的模式接近回調事件? – Popnoodles

+0

不幸的是,我不能讓模式在第一時間開火。在模式關閉的回調中,我將執行添加到圖表的代碼。需要在添加項目之前觸發模式。 – uacyber

回答

0

我覺得混亂位是針尖對每個產品每個模態數據格。所以給每個模態數據div一個唯一的ID和添加到圖表我添加了一個switch語句來激發正確的模態。

所以模態數據現在的HTML有一個ID;

<div id="op1Dialog" class="dialog_content" title="" style=""> 

現在添加到聊天中有以下內容;

function addToChart($item) { 
      var currentId = $item.attr('id'); 

        switch(currentId) { 
         case "100400": $('#op1Dialog').dialog('open'); break; 
         case "100401": $('#op2Dialog').dialog('open'); break; 
         and so on... 
        } 
       Rest of the function obove.............. 

(EDIT)FIDDLE LINK

讓我知道如果任何人都可以想到的不是使用switch語句一個更好的辦法...