根据会员日期计算费用

时间:2018-07-27 01:39:45

标签: javascript jquery

玩家在我们的活动中注册。我在(#P1#P2等中输入他们的名字,然后函数getCCCRexpire(ele)查找他们的俱乐部到期日期。我想修改函数calcEntryFee以计算入场费。如果玩家的到期日期晚于今天,则费用(#E1#E2等)为$3。如果玩家的到期日期是今天或更早,则费用为$5

问题在于,成员资格在今天或更早之前到期的非空白玩家不会被选中。例如,NAME = "Jones, John" MEMBERSHIP_EXPIRES = "2017.01.12"应该用"$5"填充费用,但只会留空。

第二个问题是函数calcEntryFee不必要的复杂性。我从未见过如此先进或坦率的混淆。我肯定会感谢有人可以重写此功能并使它正常工作。我显然在头上。我什至没有解决方案。

HTML

function calcEntryFee(elem) {
  var idx, member, exDate, today, fee;

  elem = elem || {};
  if (/^[PEMX](\d+)$/.test(elem.id)) {
    idx = RegExp.$1;
  } else {
    return false;
  }

  member = getMemberData(jQuery('#P' + idx).val());
  mmfee = parseFloat(jQuery('#M' + idx).val());
  exDate = moment(member.cccrEXP, 'YYYY.MM.DD');
  today = moment();
  fee = '';

  if (!member) {
    fee = 0;
  }
  //if ( (exDate.isBefore( today ) || exDate == 0) && P1 > "") {fee = 5; } // my attempt to solve the issue. 
  if ((exDate.isBefore(today) || !member)) {
    fee = 5;
  }
  if (exDate.isSameOrAfter(today)) {
    fee = 3;
  }
  if (member.cccrEXP == "" && mmfee > 0) {
    fee = 0;
  }

  // Updates the entry fee input value.
  jQuery('#E' + idx).val(fee);

  return fee;
}

function getCCCRexpire(ele) {
  var i = null;
  for (i = 0; cccr_mems.length > i; i++) {
    if (cccr_mems[i].Name == ele.value) {
      return cccr_mems[i].cccrEXP;

    }
  }
  return;
}

for (let i = 0; i <= 48; i++) {
  $("#P" + i).on("blur", function() {
    $("#CCCR_X" + i).val(getCCCRexpire(this));
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js"></script>
<div>
  <div><input type="text" id="P1" onblur="calcEntryFee(this);" name="N1" /></div>
  <div><input type="text" onblur="getCCCRexpire()" name="cccr_exp" id="CCCR_X1" /> </div>
  <div><input type="text" name="ef-fee" id="E1" /></div>
</div>

<div>
  <div><input type="text" id="P2" onblur="calcEntryFee(this);" name="N2" /></div>
  <div><input type="text" onblur="getCCCRexpire()" name="cccr_exp" id="CCCR_X2" /> </div>
  <div><input type="text" name="ef-fee" id="E2" /></div>
</div>

1 个答案:

答案 0 :(得分:3)

  

重写此功能并使其正常工作

(我忘了说)尝试一下:

#Create New dataframe
values <- reactiveValues()
values$cal_distdf <- data.frame(Odovalue = numeric(0))

 #Function to calculate distance

distcal <- reactive({

   newdistvalue <- df1$Odovalue #Store the new value to a variable
   newrow <- isolate(c(newdistvalue)) 
   isolate( values$cal_distdf [nrow( values$cal_distdf ) + 1,] <- c(newLine)) #append new row to the existing dataframe. NOTE using rbind() changes the column name.
   newdistvalue1 <-  values$cal_distdf #Copying appended rows dataframe to new data frame. 
   cal_dist <- as.numeric(tripd$Odovalue[nrow(newdistvalue1)]) - as.numeric(newdistvalue1$Odovalue[1]) # Calculating difference between new appended row with the first row value to Calculate distance
  return(cal_dist)

})

 output$Distance <- renderText({distcal()})

更新

  1. 添加了function calcEntryFee( ele ) { let id_num = ele.id.substring(1); // 2 if `id` is E2 // If player name is blank, empties the "ef" field. if ( ! ele.value ) { $( '#E' + id_num ).val(''); return; } let cccrEXP = getCCCRexpire( ele ); // expiration date // no expiration date if ( ! cccrEXP ) { // if membership fee > 0, fee is $0; else, it's $5 let fee = $( '#M' + id_num ).val() > 0 ? 0 : 5; $( '#E' + id_num ).val( fee ); } else { let today = moment(), expire = moment( cccrEXP, 'YYYY.MM.DD' ); // expiration date is today or earlier; fee = $5 if ( expire.isSameOrBefore( today ) ) { $( '#E' + id_num ).val(5); } // expiration date is later than today; fee = $3 else if ( expire.isAfter( today ) ) { $( '#E' + id_num ).val(3); } } } 变量。

  2. 检查了fee。如果为空白,则清空“ ef”字段。