3

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

ジャバスクリプトはこちら

        $(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, ''];
              }
            }
          });
        });

このjsfiddleを参照してください

必要なすべての日付制限は次のとおりです。

  1. 日曜日、月曜日、土曜日はありません。終わり。
  2. 今日、明日、およびそれ以前のすべての日付は非アクティブです。終わり。

ユーザーがアラバマ州のテネシー州、ケンタッキー州を選択しない場合。

  1. 火曜日は非アクティブにする必要があります。
  2. 今日 + 2 (明後日) は非アクティブにする必要があります。

理想的には、ユーザーが を選択するまで、すべての日付が非アクティブになりますshipping_state

4

2 に答える 2

1

配列disabled_daysを使用して、無効にする日を示しました。html マークアップ内の 'onchage=""' のような構文は適切ではありません。オプションで計算される +2 日、ハンドラー内でthis.form.shipping_zip.value='';check_address('shipping'); 自分で修正する必要があります。changeminDate

$(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');
});

デモ

于 2013-02-01T09:23:33.057 に答える
1

jqfaq.comにアクセスして、より興味深いよくある質問と解決策を確認してください。そして、ここにあなたの更新された作業フィドルがあります

    $("#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) {
            var shipping_state = $("#shipping_state").val();
            switch (shipping_state) {
                case "AL":
                case "KY":
                case "TN":
                    var current = new Date();
                    var today = new Date(current.getFullYear(), current.getMonth(), current.getDate());
                    if (date.valueOf() <= today.valueOf() + 2) return [false, ''];
                    else if (date.getDay() === 0 || date.getDay() === 1 || date.getDay() === 6) {
                        return [false, ''];
                    } else {
                        return [true, ''];
                    }
                    break;
                default:
                    if (date.getDay() === 0 || date.getDay() === 1 || date.getDay() === 2 || date.getDay() === 6) {
                        return [false, ''];
                    } else {
                        return [true, ''];
                    }
            }
        }
    });
于 2013-02-01T09:24:10.317 に答える