javascript - 如何将单选按钮值从表单发送到电子邮件

标签 javascript php html css

<script>
var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the crurrent tab

function showTab(n) {
  // This function will display the specified tab of the form...
  var x = document.getElementsByClassName("tab");
  x[n].style.display = "block";
  //... and fix the Previous/Next buttons:
  if (n == 0) {
    document.getElementById("prevBtn").style.display = "none";
  } else {
    document.getElementById("prevBtn").style.display = "inline";
  }
  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").innerHTML = "Submit";
  } else {
    document.getElementById("nextBtn").innerHTML = "Next";
  }
  //... and run a function that will display the correct step indicator:
  fixStepIndicator(n)
}

function nextPrev(n) {
  // This function will figure out which tab to display
  var x = document.getElementsByClassName("tab");
  // Exit the function if any field in the current tab is invalid:
  if (n == 1 && !validateForm()) return false;
  // Hide the current tab:
  x[currentTab].style.display = "none";
  // Increase or decrease the current tab by 1:
  currentTab = currentTab + n;
  // if you have reached the end of the form...
  if (currentTab >= x.length) {
    // ... the form gets submitted:
    document.getElementById("regForm").submit();
    return false;
  }
  // Otherwise, display the correct tab:
  showTab(currentTab);
}

function validateForm() {
  // This function deals with validation of the form fields
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  // A loop that checks every input field in the current tab:
  for (i = 0; i < y.length; i++) {
    // If a field is empty...
    if (y[i].value == "") {
      // add an "invalid" class to the field:
      y[i].className += " invalid";
      // and set the current valid status to false
      valid = false;
    }
  }
  // If the valid status is true, mark the step as finished and valid:
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  }
  return valid; // return the valid status
}

function fixStepIndicator(n) {
  // This function removes the "active" class of all steps...
  var i, x = document.getElementsByClassName("step");
  for (i = 0; i < x.length; i++) {
    x[i].className = x[i].className.replace(" active", "");
  }
  //... and adds the "active" class on the current step:
  x[n].className += " active";
}
</script>
<div class="tab"><h4>Zip Code</h4>
    <p><input type="text" class="text1" placeholder="Enter zip code..." oninput="this.className = ''" name="zipcode"></p>

  </div>
 <div class="tab"><h4>How long have you been planning to sell your property?</h4>
    <p><input type="radio" name="rdb" value="1-3 months"  class="option-input radio" checked/> 1-3 months</p>
    <p><input type="radio" name="rdb" value="4-6 months" class="option-input radio"> 4-6 months</p>
    <p><input type="radio" name="rdb" value="more than 6 months" class="option-input radio"> more than 6 months</p>
    
  </div>
  <div class="tab"><h4>Type of property?</h4>
    <p><input type="radio" name="rdb1" value="Single Family House" class="option-input radio"  checked/> Single Family House</p>
    <p><input type="radio" name="rdb1" value="Condo" class="option-input radio"> condo</p>
    <p><input type="radio" name="rdb1" value="Mobile" class="option-input radio">Mobile</p>
    <p><input type="radio" name="rdb1" value="Townhouse" class="option-input radio">Townhouse</p>
    <p><input type="radio" name="rdb1" value="Multi-units" class="option-input radio">Multi-units</p>
   
  </div>

我没有在电子邮件中收到单选按钮值。所有其他文本框、电子邮件和电话都通过运行上面的代码显示,但主要问题在于单选按钮。

如果我评论 radio 标签然后代码运行完美, radio 我只收到没有任何数据的电子邮件。也请检查单选按钮的值,它是否包含两个单词之间的空格是,那么它是如何工作的?

最佳答案

获取单选按钮的值

var rdb = document.getElementsByName('rdb').value;

document.querySelector('input[name="rdb"]:checked').value

关于javascript - 如何将单选按钮值从表单发送到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48639709/

相关文章:

html - 如何使用 Kuchiki 获取 HTML 文档的所有文本(除了 script/style/noscript 标签)?

php - 如何使用 PHP 正则表达式将属性添加到第一个 P 标签?

javascript - 如何获取文本框中以逗号分隔的多个值

html - 使用 CSS Counter 为标题编号

javascript - 模拟在 Windows 中打开粘滞键

javascript - rails 5 : use link_to target an mp3 (as a model object) as well as a separate div with a different attribute of the same object?

javascript - 类型错误 : Cannot read property 'gmail' of undefined

javascript - 返回一个包含来自 Observable<{string; 的响应的数组数字;字符串;}[]>

php - 从变量调用静态函数

php - 在一页中绘制多个 flot