2012-12-01 45 views
2
$(document).ready(function() { 
$.validator.setDefaults({ 
    submitHandler: function() { 
     $('#search').submit(function() { 
      var city = $("#city").val(); 
      var adults = $("#adults").val(); 
      var children = $("#children").val(); 
      var SDate = $("#hotelStartDate").val(); 
      var EDate = $("#hotelEndDate").val(); 
      SDate = SDate.substr(6, 4) + '-' + SDate.substr(3, 2) + '-' + SDate.substr(0, 2); 
      EDate = EDate.substr(6, 4) + '-' + EDate.substr(3, 2) + '-' + EDate.substr(0, 2); 
      jQuery(":submit", this).css("display", "none"); 
      jQuery(":submit", this).after("<span class='button nice awesome green large radius'>Searching...</span>"); 
      location.href = 'http://www.dhitrax.com/destination.php?checkin=' + SDate + '&checkout=' + EDate + '&city=' + city + '&adults=' + adults + '&children=' + children; 
      return false; 
     }); 
    }, 
    highlight: function (input) { 
     $(input).addClass("ui-state-highlight"); 
    }, 
    unhighlight: function (input) { 
     $(input).removeClass("ui-state-highlight"); 
    } 
}); 
$().ready(function() { 
    $.fn.themeswitcher && $('<div/>').css({ 
     position: "absolute", 
     right: 10, 
     top: 10 
    }).appendTo(document.body).themeswitcher(); 
    $("#search").validate({ 
     rules: { 
      city: "required", 
      hotelStartDate: "required", 
      hotelEndDate: "required" 
     }, 
     messages: { 
      City: "Please type your destination", 
      hotelStartDate: "Select check in Date", 
      hotelEndDate: "Select check out Date" 
     } 
    }); 
}); 
}); 
+2

WTH..add成的jsfiddle .. – thecodejack

+0

你應該格式化你的代碼正確 –

+0

你面對的問題/錯誤。 。此代碼對回答 –

回答

0

你不應該一個事件處理程序綁定到submit事件內的現有submitHandler jQuery驗證提供。由於您第一次提交,處理器被綁定,並且您第二次提交處理程序被執行,因此需要兩次點擊才能提交​​表單。

這應該是作爲移動你的代碼到submitHandler簡單:

$.validator.setDefaults({ 
    submitHandler: function() { 
     var city = $("#city").val(); 
     var adults = $("#adults").val(); 
     var children = $("#children").val(); 
     var SDate = $("#hotelStartDate").val(); 
     var EDate = $("#hotelEndDate").val(); 
     SDate = SDate.substr(6, 4) + '-' + SDate.substr(3, 2) + '-' + SDate.substr(0, 2); 
     EDate = EDate.substr(6, 4) + '-' + EDate.substr(3, 2) + '-' + EDate.substr(0, 2); 
     jQuery(":submit", this).css("display", "none"); 
     jQuery(":submit", this).after("<span class='button nice awesome green large radius'>Searching...</span>"); 
     location.href = 'http://www.dhitrax.com/destination.php?checkin=' + SDate + '&checkout=' + EDate + '&city=' + city + '&adults=' + adults + '&children=' + children; 
    }, 
    highlight: function (input) { 
     $(input).addClass("ui-state-highlight"); 
    }, 
    unhighlight: function (input) { 
     $(input).removeClass("ui-state-highlight"); 
    } 
}); 
+0

謝謝安德魯,但它不顯示搜索button.please幫助我,因爲我是新手 – Prashanth

+0

很難說爲什麼沒有看到你的標記。此外,頁面正在立即重定向,因此下一頁加載速度如此之快以至於看不到它? –