如果未选中,则设置复选框输入的值

时间:2018-08-09 06:43:46

标签: javascript html

我试图根据是否已选中复选框输入来更改复选框输入的值。这是我所拥有的:

HTML:

<input type="checkbox" id="chkPrint" name="Print" value="false" onclick="myFunction()">

JavaScript:

function myFunction() {
  var chkPrint = document.getElementById("chkPrint");
    if (chkPrint.checked == true) {
      chkPrint.value = "true";
    }
  }
function yourFunction() {
  if (document.getElementById("chkPrint").checked == false) {
     document.getElementById("chkPrint").value = "false";
  }
}
yourFunction()

当我在Chrome DevTools中查看HTML时,看到的值更改为true。但是,如果我取消选中它,那么该值将保持为真。

7 个答案:

答案 0 :(得分:1)

您足够亲密:

function myFunction() {
  var chkPrint = document.getElementById("chkPrint");
  chkPrint.value = chkPrint.checked;
  console.log('value', chkPrint.value);
}
myFunction()
<input type="checkbox" id="chkPrint" name="Print" value="false" onclick="myFunction()">

尝试使用可以处理多个复选框的脚本:

let setValue = function(e) {
  this.value = this.checked;
  console.log('value', this.value);
};

document.addEventListener('DOMContentLoaded', function() {
  console.log('page loaded...');
  document.querySelectorAll('[name=item]').forEach(function(chk) {
    chk.addEventListener('click', setValue);
    setValue.bind(chk).call(); /* set true/false on page load */
  });
});
<input type="checkbox" name="item" /><input type="checkbox" name="item" /><input type="checkbox" name="item" checked=''/><input type="checkbox" name="item" />

答案 1 :(得分:0)

首先使chkPrint值在每次单击时均为false,如果选中则仅使其为true。

 function myFunction() {
                var chkPrint = document.getElementById("chkPrint");
                chkPrint && chkPrint.value = "false";
                if (chkPrint.checked == true) {
                    chkPrint.value = "true";
                }
            }

答案 2 :(得分:0)

#$excludedusers = @('HealthMailbox123456')
Get-ADUser -Server $test -Credential $1cred -Filter{enabled -eq $true} | Where-Object { $_.DistinguishedName -notlike '*OU=.Service Accounts,*' } | Select-object Samaccountname,surname,givenname | Where { $excludedusers -NotContains$_.Samaccountname }

medium documentation

或者您可以简单地合并这些功能并分配给Event onchange。

如果要处理表单中的服务器端值。请多多讨论。 https://www.w3schools.com/jsref/event_onchange.asp

答案 3 :(得分:0)

<input type="checkbox" id="chkPrint" name="Print" value="false" onclick="myFunction();yourFunction()">
<script>
function myFunction() {
            var chkPrint = document.getElementById("chkPrint");
            if (chkPrint.checked == true) {
                chkPrint.value = "true";
                console.log(chkPrint.value)
            }
        }
function yourFunction() {
  if (document.getElementById("chkPrint").checked == false) {
     document.getElementById("chkPrint").value = "false";
      console.log(chkPrint.value)
  }
}

</script>

这是因为您仅在默认情况下调用了一个函数“ myFunction()” onclick事件,而在默认情况下又调用了另一个函数。您需要同时调用这两个函数,以便同时检查真假。

但是可以像这样轻松地优化

<input type="checkbox" id="chkPrint" name="Print" value="false" onclick="myFunction()">
<script>
function myFunction() {
            var chkPrint = document.getElementById("chkPrint");
            if (chkPrint.checked == true) {
                chkPrint.value = "true";
                console.log(chkPrint.value)
            }
            else{
             document.getElementById("chkPrint").value = "false";
                         console.log(chkPrint.value)
            }
        }


</script>

答案 4 :(得分:0)

如果我理解您的问题,我认为这段代码将解决您的问题。

function myFunction(self){
  if(self.checked == true){
      self.value = true;
    }else{
       self.value = false;
    }
}
<input type="checkbox" id="chkPrint" name="Print" value="false" onclick="myFunction(this)">

答案 5 :(得分:0)

只需将事件更改为onchange,然后检查目标值

function myFunction() {
    this.event.target.value = this.event.target.checked;    
 }
<input type="checkbox" id="chkPrint" name="Print" value="false" onchange="myFunction()">

答案 6 :(得分:0)

此代码将起作用:

<!DOCTYPE html>
<html>
<body>

Checkbox: <input type="checkbox" id="myCheck" onclick="check()">


<script>
function check() {
    if (document.getElementById("myCheck").checked){

      document.getElementById("myCheck").checked = true;
      alert ("Checked and Value is -->"+document.getElementById("myCheck").checked);
    }
    else
    {
      document.getElementById("myCheck").checked = false;
      alert ("Unchecked and Value is -->"+document.getElementById("myCheck").checked);
    }
}


</script>

</body>
</html>