gpt4 book ai didi

jquery - 如何使用 Jquery datepicker 禁用基于表单字段和全局变量的一周中的某一天?

转载 作者:行者123 更新时间:2023-12-01 01:25:39 26 4
gpt4 key购买 nike

我试图根据全局变量禁用一周中的某些天,该全局变量通过 javascript 设置,但最初由用户通过表单字段 (shipping_state) 填充。

    <form id="myform">
<p>State: <select id="shipping_state" onchange="this.form.shipping_zip.value='';check_address('shipping');" name="shipping_state" class="txtBoxStyle">
<option value=""></option><option value="AL">Alabama</option><option value="AK">Alaska</option><option value="AZ">Arizona</option><option value="AR">Arkansas</option><option value="CA">California</option><option value="CO">Colorado</option><option value="CT">Connecticut</option><option value="DE">Delaware</option><option value="DC">District of Columbia</option><option value="FL">Florida</option><option value="GA">Georgia</option><option value="HI">Hawaii</option><option value="ID">Idaho</option><option value="IL">Illinois</option><option value="IN">Indiana</option><option value="IA">Iowa</option><option value="KS">Kansas</option><option value="KY">Kentucky</option><option value="LA">Louisiana</option><option value="ME">Maine</option><option value="MD">Maryland</option><option value="MA">Massachusetts</option><option value="MI">Michigan</option><option value="MN">Minnesota</option><option value="MS">Mississippi</option><option value="MO">Missouri</option><option value="MT">Montana</option><option value="NE">Nebraska</option><option value="NV">Nevada</option><option value="NH">New Hampshire</option><option value="NJ">New Jersey</option><option value="NM">New Mexico</option><option value="NY">New York</option><option value="NC">North Carolina</option><option value="ND">North Dakota</option><option value="OH">Ohio</option><option value="OK">Oklahoma</option><option value="OR">Oregon</option><option value="PA">Pennsylvania</option><option value="PR">Puerto Rico</option><option value="RI">Rhode Island</option><option value="SC">South Carolina</option><option value="SD">South Dakota</option><option value="TN">Tennessee</option><option value="TX">Texas</option><option value="UT">Utah</option><option value="VT">Vermont</option><option value="VI">Virgin Islands</option><option value="VA">Virginia</option><option value="WA">Washington</option><option value="WV">West Virginia</option><option value="WI">Wisconsin</option><option value="WY">Wyoming</option></select>
</p>

<p>Delivery Date:
<input name="my_deliverydate" type="text" id="datepicker"
size="30" />
</p>
<p>ALT Delivery Date:
<input name="my_altdeliverydate" type="text" id="altdatepicker"
size="30" />
</p>

<p>
Customer Comments:<br><textarea class="txtBoxStyle" id="custcomment" cols="55" rows="3"></textarea>
</p>
<p>
Combined Comment Field:<br><textarea class="txtBoxStyle" name="ocomment" id="compcomment" cols="55"
rows="3"></textarea>
</p>

<input type="button" name="submit" class="button" id="submit" value="Send" onclick="$('#compcomment').val('Delivery Date: ' + $('#altdatepicker').val() + ', Customer Comments: ' + $('#custcomment').val());"/>

这是 JavaScript

        $(function () {
var date = new Date();
var currentMonth = date.getMonth(); // current month
var currentDate = date.getDate()+1; // current date
var currentYear = date.getFullYear(); //this year
$("#datepicker").datepicker({
dateFormat: "DD, d MM, yy", // set main date format to Wednesday, January 10th, 2013
altFormat: "yy-mm-dd", // set alt format to default
altField: "#altdatepicker", //set alt date field
changeMonth: true, // this will allow users to chnage the month
changeYear: true, // this will allow users to chnage the year
minDate: new Date(currentYear, currentMonth, currentDate),
beforeShowDay: function (date) {
if (date.getDay() === 0 || date.getDay() === 1 || date.getDay() === 2 || date.getDay() === 6) {
return [false, ''];
} else {
return [true, ''];
}
}
});
});

See this jsfiddle

以下是我需要的所有日期限制:

  1. 没有周日、周一或周六。完成。
  2. 今天、明天以及之前的所有日期均无效。完成。

如果用户没有选择田纳西州、肯塔基州、阿拉巴马州。

  1. 周二需要处于非事件状态。
  2. 今天 + 2(后天)需要处于非事件状态。

理想情况下,在用户选择 shipping_state 之前,所有日期都将处于非事件状态。

最佳答案

我使用数组 disabled_days 来指示要禁用的日期。html 标记内的像 'onchage=""' 这样的语法不好,您需要自己修复 this.form.shipping_zip.value='';check_address('shipping'); change 处理程序中,+2 天在 minDate 选项中计算。

$(function () {
$('#shipping_state').change(function () {
//uncomment later;
//this.form.shipping_zip.value='';check_address('shipping');
var val = $(this).val();
if ($.inArray(val, ['AL', 'KY', 'TN']) > -1) {
disabled_days = [0, 1, 6];
return;
}
disabled_days = [0, 1, 2, 6];
});
var disabled_days = [0, 1, 2, 3, 4, 5, 6],
date = new Date(),
currentMonth = date.getMonth(), // current month
currentDate = date.getDate(), // current date
currentYear = date.getFullYear(), //this year
dp_config = {
dateFormat: "DD, d MM, yy", // set main date format to Wednesday, January 10th, 2013
altFormat: "yy-mm-dd", // set alt format to default
altField: "#altdatepicker", //set alt date field
changeMonth: true, // this will allow users to chnage the month
changeYear: true, // this will allow users to chnage the year
minDate: new Date(new Date(currentYear, currentMonth, currentDate).getTime() + 86400000 * 2),
beforeShowDay: function (date) {
if ($.inArray(date.getDay(), disabled_days) > -1) return [false, ''];
return [true, ''];
}
};

$("#datepicker,#altdatepicker").datepicker(dp_config);
$('#shipping_state').trigger('change');
});

DEMO

关于jquery - 如何使用 Jquery datepicker 禁用基于表单字段和全局变量的一周中的某一天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14641946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com