禁用单选按钮检查

时间:2019-10-05 11:04:02

标签: javascript html

早上好, 是否有任何JavaScript方法可用于“保修”单选按钮检查,因为如果购买日期超过24个月,则应禁用“保修”。

我创建“(购买日期和维修日期)”,维修日期必须晚于该零件的购买日期,并且可以100%工作

如果购买日期超过24个月,则仅需要“保修”方面的帮助,我应该被禁用。 预先感谢您的帮助。

$(document).ready(function() {
  var today = new Date();
  $('.dt1').datepicker({
    format: 'mm-dd-yyyy',
    autoclose: true,
    endDate: "today",
    maxDate: today
  }).on('changeDate', function(date) {
    $(this).datepicker('hide');
    var date2 = $('.dt1').datepicker('getDate');
    date2.setDate(date2.getDate() + 1);
    $('.dt2').datepicker('setDate', date2);
    //sets minDate to dt1 date + 1
    $('.dt2').datepicker('option', 'minDate', date2);
  });

  $('.dt2').datepicker({
    Format: 'mm-dd-yyyy',
    endDate: "today",
    maxDate: today,
    onClose: function() {
      var dt1 = $('.dt1').datepicker('getDate');
      console.log(dt1);
      var dt2 = $('.dt2').datepicker('getDate');
      if (dt2 <= dt1) {
        var minDate = $('.dt2').datepicker('option', 'minDate');
        $('.dt2').datepicker('setDate', minDate);
      }
    }
  });
  var a = moment('1/1/2019', 'DD/MM/YYYY');
  var b = moment('getMonth()', 'DD/MM/YYYY');
  var months = b.diff(a, 'months');
  $('#res').text(months);

});
#repair {
  margin-left: 25em;
}

#purchase {
  margin-left: 10em;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

<!--Purchase Date-->
<div id="purchase">
  <label>Purchase Date:</label>
  <div>
    <input type="text" id="purchaseDate" class="dt1" />
  </div>
</div>

<!--Repair Date-->
<div id="repair">
  <label>Repair Date:</label>
  <div>
    <input type="text" id="repairDate" class="dt2" />
  </div>
</div>

<!--Warranty-->
<div id="radios1">
  <p>Under Warranty</p>
  <label class="form-check-label" for="gridRadios3">Warranty: </label>
  <input class="check-input" type="radio" name="gridRadios" id="gridRadios3" value="option1"><br>
</div>

2 个答案:

答案 0 :(得分:1)

据我所知,您可以检查每个日期更改事件的日期差:

$(document).ready(function() {

    function isUnderWarranty() {
    let rDate = new Date($('#repairDate').val());
    let pDate = new Date($('#purchaseDate').val());
    console.log(rDate);
    console.log(pDate);
    let months24_ms = 1000 * 60 * 60 * 24 * 30.5 * 24;
    if(rDate - pDate < months24_ms) {
        console.log('ena');
        document.body.querySelector('#gridRadios3').disabled = false;
    } else {
        console.log('disa')
      document.body.querySelector('#gridRadios3').disabled = true;
    }
  }

  var today = new Date();
  $('#purchaseDate').datepicker({
    format: 'mm-dd-yyyy',
    onClose: isUnderWarranty
  });

  $('#repairDate').datepicker({
    Format: 'mm-dd-yyyy',
    onClose: isUnderWarranty
  });
});

答案 1 :(得分:0)

在下面的示例中,我将您要尝试的工作分解为单独的部分。

这将有助于保持您的代码井井有条和可管理性,并且我将其更改为2年而不是24个月,因为月份的长度有所不同,并且使比较变得复杂,而且还有很多天数可用。

希望这会有所帮助,并为您提供如何更好地组织代码的想法。

#!/bin/bash
#
# Sample script illustrating optional arguments with bash getopts in
#   a style similar to mysql client.  "--" is also supported.
#
# tgage@nobigfoot.com - 5-Oct-2019 - Distribute/use without restriction.
#

# Defaults
username="$(whoami)"
unset password

while getopts ":u:p:" arg; do
  case $arg in
      u)  # Username
      username="$OPTARG"
      ;;
      p)  # Password
      thisarg=$((OPTIND - 1)); thisarg="${!thisarg}"; thisarg="${thisarg:2}"

      if [[ ! -z $OPTARG ]] && [[ "$OPTARG" != "$thisarg" ]]; then
          unset password
          OPTIND=$((OPTIND - 1))
      else
          password="$thisarg"
      fi
      if [[ -z $password ]]; then
          read -s -p "Password: " password
          echo
      fi
      ;;
      \?)
      # Request Help
      echo "Sorry that I can be of no help."
      ;;
      *)
      # Deal with option at end of line
      OPTION=$((OPTIND - 1)); OPTION="${!OPTION}"
      case $OPTION in
          --)
          # Add support for --
          break
          ;;
          -p)
          # Option with optional argument
          unset password
          read -s -p "Password: " password
          echo
          ;;
          *)
          # Invalid argument
          echo "Error; Invalid option \"$OPT\"." >&2
          ;;
      esac
      ;;
  esac
done
shift $((OPTIND -1))

echo "Username: $username"
echo "Password: $password"
echo
echo "Remaining arguments:$*"
echo
var pd = $('#purchaseDate');
var rd = $('#repairDate');
var war = $('#gridRadios3');

$(function(){
	initDatePicker();

	pd.on('change',function(){
		var date = $(this).datepicker('getDate');
		updateRepairDatePicker(date);
		updateWarrantyRadio(date);
	});
});

function updateRepairDatePicker(date){
	date.setDate(date.getDate()+1);
	rd.datepicker('setDate', date);
	rd.datepicker('option', {minDate: date, disabled:false});
}

function updateWarrantyRadio(date){
	var today = new Date();
	//var difference_in_days = (today - date) / (1000 * 3600 * 24);
	var difference_in_years = (today - date) / (1000 * 3600 * 24 * 365);
	war.prop('disabled',  difference_in_years > 2);
}

function initDatePicker(){
	var today = new Date();
	pd.datepicker({
        format: 'mm-dd-yyyy',
        endDate: "today",
        maxDate: today
    });
    rd.datepicker({
        format: 'mm-dd-yyyy',
        disabled:true
    });
}
.dt {
  margin-left: 1em;
  display:inline-block;
}