HTML / Javascript表单复选框值未显示

时间:2016-12-31 19:23:19

标签: javascript jquery html forms

相当直接的问题。

当用户点击"添加"按钮和复选标记被选中我希望值得说出" Smoker"在班级="家庭"。

如果没有选中复选标记,我希望值得说出"非吸烟者"

目前,我的" if else"声明没有解雇。一直在寻找解决这个问题。有人可以帮忙吗?

方注意:没有jQuery,无法编辑HTML。只有纯Javascript。

HTML

 function validate(form) {

        fail = validateAge(form.age.value)
        fail += validateRel(form.rel.value)

        if (fail == "") return true
        else {
            alert(fail);
            return false
        }
    }

    function validateAge(field) {
        if (isNaN(field)) return "No age was entered. \n"
        else if (field < 1 || field > 200)
            return "Age must be greater than 0. \n"
        return ""
    }

    function validateRel(field) {
        if (field == "") return "Please select a relationship \n"
        return ""

    }


    document.querySelector("form").onsubmit = function() {
        return validate(this)

    }



    document.querySelector(".add").onclick = function(event) {
        event.preventDefault();
        createinput()


    }


    count = 0;

    function createinput() {
        field_area = document.querySelector('.household')
        var li = document.createElement("li");
        var p1 = document.createElement("p");
        var p2 = document.createElement("p");
        var p3 = document.createElement("p");
        var x = document.getElementsByName("age")[0].value;
        var y = document.getElementsByName("rel")[0].value;
        var z = document.getElementsByName("smoker")[0].value.checked;


        if( z === undefined) {
            return ("Non smoker \n")

        }
        else {
           return ("Smoker \n")

        }


        p1.innerHTML = x;
        p2.innerHTML = y;
        p3.innerHTML = z;
        li.appendChild(p1);
        li.appendChild(p2);
        li.appendChild(p3);
        field_area.appendChild(li);
        //removal link
        var removalLink = document.createElement('a');
        removalLink.onclick = function() {
            this.parentNode.parentNode.removeChild(this.parentNode)
        }
        var removalText = document.createTextNode('Remove Field');
        removalLink.appendChild(removalText);
        li.appendChild(removalLink);
        count++
    }

JS

SyncUser.login

2 个答案:

答案 0 :(得分:5)

第一个问题是你没有正确地获得复选框状态。它应该是document.getElementsByName("smoker")[0].checked而不是document.getElementsByName("smoker")[0].value.checked

第二个问题是您在return中使用了if else。如果使用return,则不会执行以下函数代码。

更改

var z = document.getElementsByName("smoker")[0].value.checked;

if( z === undefined) {
    return ("Non smoker \n")
}
else {
    return ("Smoker \n")
}

var z = document.getElementsByName("smoker")[0].checked;

if (!z) {
    z = "Non smoker \n";
} else {
    z = "Smoker \n";
}

答案 1 :(得分:2)

因为您只有一个“家庭”元素,所以可以添加以下处理程序:

document.querySelectorAll('[name="smoker"]')[0].onclick = function() {
 document.getElementsByClassName('household')[0].textContent = 
                  this.checked ? 'Smoker' : 'Non-smoker';

}

示例:

function validate(form) {

  fail = validateAge(form.age.value)
  fail += validateRel(form.rel.value)

  if (fail == "") return true
  else {
    alert(fail);
    return false
  }
}

function validateAge(field) {
  if (isNaN(field)) return "No age was entered. \n"
  else if (field < 1 || field > 200)
    return "Age must be greater than 0. \n"
    return ""
}

function validateRel(field) {
  if (field == "") return "Please select a relationship \n"
  return ""

}


count = 0;

function createinput() {
  field_area = document.querySelector('.household')
  var li = document.createElement("li");
  var p1 = document.createElement("p");
  var p2 = document.createElement("p");
  var p3 = document.createElement("p");
  var x = document.getElementsByName("age")[0].value;
  var y = document.getElementsByName("rel")[0].value;
  var z = document.getElementsByName("smoker")[0].checked;


  if( !z ) {
    z =  "Non smoker";

  }
  else {
    z = "Smoker";

  }


  p1.innerHTML = x;
  p2.innerHTML = y;
  p3.innerHTML = z;
  li.appendChild(p1);
  li.appendChild(p2);
  li.appendChild(p3);
  field_area.appendChild(li);
  //removal link
  var removalLink = document.createElement('a');
  removalLink.onclick = function() {
    this.parentNode.parentNode.removeChild(this.parentNode)
  }
  var removalText = document.createTextNode('Remove Field');
  removalLink.appendChild(removalText);
  li.appendChild(removalLink);
  count++
}

document.querySelector("form").onsubmit = function() {
  return validate(this)

}



document.querySelector(".add").onclick = function(event) {
  event.preventDefault();
  createinput()


}

document.querySelectorAll('[name="smoker"]')[0].onclick = function() {
  document.getElementsByClassName('household')[0].textContent = this.checked ? 'Smoker' : 'Non-smoker'
}
<ol class="household"></ol>
<form>
    <div>
        <label>Age
            <input type="text" name="age">
        </label>
    </div>
    <div>
        <label>Relationship
            <select name="rel">
                <option value="">---</option>
                <option value="self">Self</option>
                <option value="spouse">Spouse</option>
                <option value="child">Child</option>
                <option value="parent">Parent</option>
                <option value="grandparent">Grandparent</option>
                <option value="other">Other</option>
            </select>
        </label>
    </div>
    <div>
        <label>Smoker?
            <input type="checkbox" name="smoker">
        </label>
    </div>
    <div>
        <button class="add">add</button>
    </div>
    <div>
        <button type="submit">submit</button>
    </div>
</form>