提交运行甚至停止传播

时间:2013-11-05 12:47:23

标签: jquery

我想滑动切换表单,仅在有人想要使用表单时显示。提交代码在尝试切换表单时运行。我已经使用了停止传播,但它无法正常工作。

$(document).ready(function() {//ready function

    if($('#calculator')) {//if this id exists on the page

        $('#calculatorInputs').css('display','none');                                            
        $('#calculator h3').click(function() {

            $('#calculatorInputs').slideToggle('slow', function() {
            // Animation complete.
            });
        }); 


        $('#Original_Wattage').addClass('focusOn');         
        $("input").focus(function(){//applies a background color to current input
        $(this).addClass('focusOn');
        });         

        $("input").focusout(function(){//removes background color from current input
        $(this).removeClass('focusOn');
        }); 

        $('input').focusout(function() {//start of focusout
                $validateThis = $(this).attr("id");
                $currentValue = $(this).val();  

                switch ($validateThis) {//validates the entries in the various form fields
                    case 'Replacement_Wattage':
                        if($currentValue < 1 || $currentValue > 2000 || $currentValue == "" || isNaN($currentValue) )   {
                            alert('Please enter a Replacement Fixture Wattage between 1 and 2000');
                            setTimeout(function() { $('#Replacement_Wattage').focus(); },50);//setTimeOut
                        }
                     break;

                case 'Original_Wattage':
                        if($currentValue < 1 || $currentValue > 2000 || isNaN($currentValue) )  {
                            setTimeout(function() { $('#Original_Wattage').focus(); },50);//setTimeOut
                            alert('Please Replace the Original Lamp Wattage between 1 and 2000');                           
                        }
                        break;
                case 'Number_of_Lamps':
                        if($currentValue < 1 || isNaN($currentValue) )  {
                            setTimeout(function() { $('#Number_of_Lamps').focus(); },5);//setTimeOut
                            alert('Please input the number of lamps.'); 
                        }
                        break;     
                case 'Hours':
                        if($currentValue < 1 || $currentValue >24 || isNaN($currentValue) ) {
                            setTimeout(function() { $('#Hours').focus(); },5);//setTimeOut
                            alert('Please input number of hours not exceeding 24.');            
                        }
                        break;        
                case 'Days':
                        if($currentValue < 1 || $currentValue > 7 || isNaN($currentValue) ) {
                            setTimeout(function() { $('#Days').focus(); },5);//setTimeOut                   
                            alert('Please input number of days not exceeding 7.');              
                        }
                        break;                
                case 'Weeks':
                        if($currentValue < 1 || $currentValue > 52 || isNaN($currentValue) )    {
                            setTimeout(function() { $('#Weeks').focus(); },5);//setTimeOut                                  
                            alert('Please input number of weeks not exceeding 52.');                
                        }
                        break;            
                case 'Energy_Cost':
                        if($currentValue < 0 || $currentValue > .99999 || isNaN($currentValue) )    {
                            setTimeout(function() { $('#Energy_Cost').focus(); },5);//setTimeOut                            
                            alert('Please input a number between 0 and 1 in decimal form.');
                        }
                        break;
            }// end of switch statement
        });//end of input focusout code

        $("#submit").click(function(e) {//submits to update the savings
            e.preventDefault();
            if($OriginalFixtureType = $('input:radio[name="Original_Fixture_Type"]:checked').val()) {//if lamp type is checked, then wt
                $OriginalFixtureType = $('input:radio[name="Original_Fixture_Type"]:checked').val();
                $currentOriginal_Wattage =  parseInt($('#Original_Wattage').val()) * 1.05;
            } else {
                $currentOriginal_Wattage =  parseInt($('#Original_Wattage').val());
            }       

            $currentLampType = $('#Lamp_Type').val();//finds lamp type in hidden field - adjusts wattage based upon lamp type       
            if($currentLampType == "High Pressure Sodium (HPS)" || $currentLampType =="Pulse Start - Metal Halide (PS-MH)" || $currentLampType == "Metal Halide (PS-MH)") {
                $currentReplacement_Wattage  =  parseInt($('#Replacement_Wattage').val() * 1.05);           
            } else {
                $currentReplacement_Wattage  =  parseInt($('#Replacement_Wattage').val());
            }   


            if(isNaN($currentOriginal_Wattage) || $currentOriginal_Wattage < 1  )   {
                alert('Sorry you have either not entered a value for your Original Wattage OR the wattage is  not between 1 and 2000 OR  it is lower than Replacement Wattage.');
            } else {
                $kilowattsSaved =       (($currentOriginal_Wattage - $currentReplacement_Wattage    ) *  $('#Number_of_Lamps').val()  ) / 1000;             
                $kilowattsSavedPerYear =        (  $('#Hours').val()   *  $('#Days').val()  *  $('#Weeks').val() ) *        $kilowattsSaved;
                $dollarSavings  = parseFloat($('#Energy_Cost').val() * $kilowattsSavedPerYear).toFixed(2);                                              
                $validateThis = $('#Replacement_Wattage').val();        
                $('p.kilowattsSaved').text('Total Kilowatts saved are: ' + $kilowattsSaved + 'kw');
                $('p.kilowattsSavedPerYear').text('Total Kilowatts saved Per Year are: ' + $kilowattsSavedPerYear) + 'kw per year';
                $('p.annualDollarSavings').html('Annual Dollar Savings: <span class="dollars">$' + $dollarSavings) + "</span>";                 
            }
        });//end of submit

    }//end of if find calculator id on the page

});//end ready function

0 个答案:

没有答案