简体   繁体   English

更改值属性(如果选中)

[英]Change value attribute if selected

Using W3 tutorials I have some basic javascript code that will return the values of three inputs from a form. 使用W3教程,我有一些基本的JavaScript代码,这些代码将从表单中返回三个输入的值。 All I'm trying to do is change the value attribute of a checkbox when it is selected. 我要做的就是更改选中复选框的value属性。 If it is checked, I'd like to return and display true. 如果选中,我想返回并显示true。 If not, I'd like it to return and display false. 如果没有,我希望它返回并显示false。 The default value of the checkbox is false, and I am able to get page to return false always even when it's checked. 该复选框的默认值为false,即使选中该复选框,我也可以使页面始终返回false。 I have looked looked at several examples of how to change the value but have been unsuccessful in implementing it. 我看过几个有关如何更改值的示例,但是实现它们均未成功。 Please let me know how I can adjust my code so that the value attribute will be updated to true when the checkbox is selected. 请让我知道如何调整代码,以便在选中复选框时将value属性更新为true。

 function myFunction() { /* **Commented out part is what breaks the code** if (document.querySelector('checkbox1').checked) { document.getElementByID('check').value='true'; } else { document.getElementByID('check').value='false'; } */ var x = document.getElementById("frm1"); var text = ""; var i; for (i = 0; i < x.length; i++) { text += x.elements[i].value + "<br>"; } document.getElementById("demo").innerHTML = text; } 
 <form id="frm1" action="/action_page.php"> <input type="text" name="inString" value="Enter Text"> String Input<br> <input type="number" name="inNum">Number Input<br><br> <input type="checkbox" id="check" name="checkbox1" value="false"> Boolean Input<br><br> </form> <p>Click button to display the value of each element in the form.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> 

There are two issues with your code, one of which I pointed out in a comment above. 您的代码有两个问题,其中一个是我在上面的注释中指出的。 The other is that document.querySelector('checkbox1') isn't valid as checkbox1 is a name attribute. 另一个是document.querySelector('checkbox1')无效,因为checkbox1是name属性。 You can instead use document.querySelector('#check') which selects the checkbox via its ID, or document.querySelector('[name="checkbox1"]') which selects it by the name attribute. 您可以改用document.querySelector('#check')通过其ID选择复选框,也可以使用document.querySelector('[name="checkbox1"]')通过name属性选择document.querySelector('[name="checkbox1"]')

 function myFunction() { if (document.querySelector('#check').checked) { document.getElementById('check').value = 'true'; } else { document.getElementById('check').value = 'false'; } var x = document.getElementById("frm1"); var text = ""; var i; for (i = 0; i < x.length; i++) { text += x.elements[i].value + "<br>"; } document.getElementById("demo").innerHTML = text; } 
 <form id="frm1" action="/action_page.php"> <input type="text" name="inString" value="Enter Text"> String Input<br> <input type="number" name="inNum">Number Input<br><br> <input type="checkbox" id="check" name="checkbox1" value="false"> Boolean Input<br><br> </form> <p>Click button to display the value of each element in the form.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> 

What about: 关于什么:

function myFunction() {
  /* **Commented out part is what breaks the code**
  if (document.querySelector('checkbox1').checked) {
  document.getElementByID('check').value='true';
  }
  else {
  document.getElementByID('check').value='false';
  }
  */
  var x = document.getElementById("frm1");
  var text = "";
  var i;
  for (i = 0; i < x.length; i++) {
    // check if current input type is checkbox
    if( x.elements[i].type == 'checkbox' ){
    // if it is, get checked value
         text += x.elements[i].checked + "<br>"; // return true or false
    }else{
         text += x.elements[i].value + "<br>";
    }

  }
  document.getElementById("demo").innerHTML = text;
}

Hope that help you ;) 希望能对您有所帮助;)

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

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