如何使用jQuery从HTML中提取值到特定网站

问题描述 投票:-1回答:1

你能解决这个问题吗?我想要的是将值“checkin”和“checkout”的值传递给特定的URL。但它不适用于我的代码。

$('#submitBooking').on('click', function() {

  var checkin = new Date($('#checkinbooking').val());
  inDay = checkin.getDate();
  inMonth = checkin.getMonth() + 1;
  inYear = checkin.getFullYear();
  checkin = ([inYear, inMonth, inDay].join('-'));

  var checkout = new Date($('#checkoutbooking').val());
  outDay = checkout.getDate();
  outMonth = checkout.getMonth() + 1;
  outYear = checkout.getFullYear();
  checkout = ([outYear, outMonth, outDay].join('-'));

  window.location.href =
    'https://hotels.cloudbeds.com/reservation/oL7RdR?checkin=' + checkin + '&checkout=' + checkout;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="https://hotels.cloudbeds.com/reservation/oL7RdR#/post" method="post">
  <label for="checkin" class="VP"><span class="VC">From</span></label>
  <input type="date" name="checkin" id="checkinbooking">

  <label for="checkout" class="VP"><span class="VC">To</span></label>
  <input type="date" name="checkout" id="checkoutbooking">

  <input type="submit" value="Check Availability" id="submitBooking">
</form>
javascript jquery html
1个回答
0
投票

我已成功使用它。我做的是将方法改为get并使用preventDefault()和我的href的一些变化

$('#submitBooking').on('click', 
function(event){
    event.preventDefault()

    var checkin = new Date($('#checkinbooking').val());
    inDay = checkin.getDate();
    inMonth = checkin.getMonth()+1;
    inYear = checkin.getFullYear();
    checkin = ([inYear, inMonth, inDay].join('-'));

    var checkout = new Date($('#checkoutbooking').val());
    outDay = checkout.getDate();
    outMonth = checkout.getMonth()+1;
    outYear = checkout.getFullYear();
    checkout = ([outYear, outMonth, outDay].join('-'));

    window.location.href = 'https://hotels.cloudbeds.com/reservation/oL7RdR#checkin='+checkin+'&checkout='+checkout;
});
<form action="https://hotels.cloudbeds.com/reservation/oL7RdR#" method="get">
						<label for="checkin" class="VP"><span class="VC">From</span></label>
						<input type="date" name="checkin" id="checkinbooking">

						<label for="checkout" class="VP"><span class="VC">To</span></label>
						<input type="date" name="checkout" id="checkoutbooking">

						<input type="submit" value="Check Availability" id="submitBooking">
					</form>
© www.soinside.com 2019 - 2024. All rights reserved.