简体   繁体   English

根据用户年龄更改单选按钮值的问题

[英]Issue with Changing a radio button value based on a users age

I am trying to develop some JS that will allow a form to change the value of a submitted radio button based on the users age. 我正在尝试开发一些JS,以允许表单根据用户年龄更改提交的单选按钮的值。 Essentially, if the user is 13yo or younger AND selected radio1 value of '100', it should change the '100' to a blank. 本质上,如果用户为13岁或更小且选择的radio1值为'100',则应将'100'更改为空白。

Below is the code. 下面是代码。 No matter what i do, it seems to always see the user as being over the age of 13. Any help is appreciated! 无论我做什么,似乎总是会看到用户已超过13岁。感谢您的帮助!

var ofAge = "N"

function checkAge() {
  var monInput = document.querySelector("#i_date_of_birth_month").value
  var dayInput = document.querySelector("#i_date_of_birth_day").value
  var yearInput = document.querySelector("#i_date_of_birth_year").value
  var dateString = monInput + ", " + dayInput + ", " + yearInput
  var birthDate = new Date(dateString);
  var todayDate = new Date()
  var age = todayDate.getFullYear() - birthDate.getFullYear();
  var m = birthDate.getMonth() - todayDate.getMonth()
  if (m < 0) {
    age--
  }
  if (age > 13) {
    ofAge = "Y"
  } else if (age == 13) {
      var d = birthDate.getDate() - todayDate.getDate()
      if (d >= 0) {
        ofAge = "Y"
      } else {
        ofAge = "N"
      }
    }
  if (ofAge == "N") {
    document.querySelector("#radio1").value = ""
  } else {
    document.querySelector("#radio1").value = "100"
  }
}

document.getElementById("i_date_of_birth_day").addEventListener("change", checkAge)
document.getElementById("i_date_of_birth_year").addEventListener("change", checkAge)
document.getElementById("i_date_of_birth_month").addEventListener("change", checkAge)

You need to add else statement if age is less than 13. 如果年龄小于13,则需要添加else语句。

} else { ofAge = "N"; }

 var ofAge = "N" function checkAge() { var monInput = document.querySelector("#i_date_of_birth_month").value var dayInput = document.querySelector("#i_date_of_birth_day").value var yearInput = document.querySelector("#i_date_of_birth_year").value var dateString = monInput + ", " + dayInput + ", " + yearInput var birthDate = new Date(dateString); var todayDate = new Date() var age = todayDate.getFullYear() - birthDate.getFullYear(); var m = birthDate.getMonth() - todayDate.getMonth() if (m < 0) { age-- } age = parseFloat(age); if (age > 13) { ofAge = "Y" } else if (age == 13) { var d = birthDate.getDate() - todayDate.getDate() if (d >= 0) { ofAge = "Y" } else { ofAge = "N" } } else { ofAge = "N"; } if (ofAge == "N") { document.querySelector("#radio1").value = "" } else { document.querySelector("#radio1").value = "100" } } document.getElementById("i_date_of_birth_day").addEventListener("change", checkAge) document.getElementById("i_date_of_birth_year").addEventListener("change", checkAge) document.getElementById("i_date_of_birth_month").addEventListener("change", checkAge) 
 <input type="text" name="" id="i_date_of_birth_day"> <input type="text" name="" id="i_date_of_birth_month"> <input type="text" name="" id="i_date_of_birth_year"> <input type="radio" name="" id="radio1"> <input type="radio" name="" id="radio2"> 

The date string you are creating (with commas) is not in the correct format. 您创建的日期字符串(带有逗号)的格式不正确。 But it would be better to set the day, month, and year directly 但是最好直接设置日期,月份和年份

  var monInput = document.querySelector("#i_date_of_birth_month").value
  var dayInput = document.querySelector("#i_date_of_birth_day").value
  var yearInput = document.querySelector("#i_date_of_birth_year").value

This is where your problem is I believe. 我相信这就是您的问题所在。

  var dateString = monInput + ", " + dayInput + ", " + yearInput

Instead, you should create a default Date(), and use setFullYear, setMonth (warning: months in javascript are 0 based) and setDate 相反,您应该创建一个默认的Date(),并使用setFullYear,setMonth(警告:javascript中的月份基于0)和setDate

Building on @tech2017's answer , you also need to change the way you're processing the date. 基于@ tech2017的答案 ,您还需要更改处理日期的方式。 Instead of commas and spaces, use a hyphen ( - ) in between each field to build the date object. 在每个字段之间使用连字符( - )代替逗号和空格以构建日期对象。

See the code below for @tech2017's answer modified to correctly build the date. 请参阅下面的代码,以修改@ tech2017的答案以正确构建日期。 The console.log() calls are in there so you can see what's being generated and changed. console.log()调用位于其中,因此您可以查看正在生成和更改的内容。

 var ofAge = "N" function checkAge() { var monInput = document.querySelector("#i_date_of_birth_month").value var dayInput = document.querySelector("#i_date_of_birth_day").value var yearInput = document.querySelector("#i_date_of_birth_year").value var dateString = monInput + "-" + dayInput + "-" + yearInput console.log("dateString: " + dateString); var birthDate = new Date(dateString); console.log("birthDate: " + birthDate); var todayDate = new Date() var age = todayDate.getFullYear() - birthDate.getFullYear(); var m = birthDate.getMonth() - todayDate.getMonth() if (m < 0) { age-- } age = parseFloat(age); if (age > 13) { ofAge = "Y" } else if (age == 13) { var d = birthDate.getDate() - todayDate.getDate() if (d >= 0) { ofAge = "Y" } else { ofAge = "N" } } else { ofAge = "N"; } if (ofAge == "N") { document.querySelector("#radio1").value = "" } else { document.querySelector("#radio1").value = "100" } console.log("radio1 value = " + document.querySelector("#radio1").value); } document.getElementById("i_date_of_birth_day").addEventListener("change", checkAge) document.getElementById("i_date_of_birth_year").addEventListener("change", checkAge) document.getElementById("i_date_of_birth_month").addEventListener("change", checkAge) 
 <input type="text" name="" id="i_date_of_birth_day"> <input type="text" name="" id="i_date_of_birth_month"> <input type="text" name="" id="i_date_of_birth_year"> <input type="radio" name="" id="radio1"> <input type="radio" name="" id="radio2"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM