JavaScript - 使用 onClick on Button 更改布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47216669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:15:13  来源:igfitidea点击:

JavaScript - Changing Boolean Value Using onClick on Button

javascripthtml

提问by xerxes39

In here, I would wanna change the value of the input box. I would wanna change the value when user clicks on the Savebutton.

在这里,我想更改输入框的值。当用户单击Save按钮时,我想更改该值。

For example, the default value of the input box is Falsebut when user clicks on the save button, the value in the input box changes to True.

例如,输入框的默认值是,False但是当用户点击保存按钮时,输入框中的值变为True

Need help in solving the condition logic.

需要帮助解决条件逻辑。

function change() 
{

  var change = document.getElementById("check");
  if (change.value == "false") 
  {
    document.test.savereport = "True";
    document.test.submit();
  } 
  else 
  {
    change.value = "false";
  }
}
<form name="test" method="post">
  <input type="text" name="savereport" value="False" />
</form>

<div align="center">
  <button type="button" value="false" id="check" onclick="change()" />Save</button>
</div>

采纳答案by gurvinder372

Apart from removing self-closing of button tag, replace

除了去除按钮标签的自动关闭,替换

document.test.savereport = "True";

with

document.test.elements[ "savereport" ].value = "true"

Demo

演示

function change() {
  var change = document.getElementById("check");
  if (change.value == "false") {
    document.test.elements["savereport"].value = "True";
    //document.test.submit();
  } else {
    change.value = "true";
  }
}
<form name="test" method="post">
  <input type="text" name="savereport" value="False" />
</form>

<div align="center">
  <button type="button" value="false" id="check" onclick="change()"> Save </button>
</div>

回答by ggbranch

I'm rusty with the exact syntax but the following code should do

我对确切的语法生疏了,但下面的代码应该做

function change() 
{

  var change = document.getElementById("check");
  if (change.value == "1") 
  {
    change.value = false
    document.test.savereport = "True";
    document.test.submit();
  } 
  else 
  {
    change.value = true
  }
}

回答by Yash

I don't understand why you need to go through all the trouble of reading values for this particular requirement. You are reading the value, comparing it to False, leaving it as is if so or if True, you change it to False. Hence essentially, you are just leaving the value to be Falseno matter what the earlier value was.

我不明白为什么您需要为这个特定要求经历读取值的所有麻烦。您正在读取值,将其与 进行比较False,如果是,则保持原样,如果 ,则将True其更改为False。因此,本质上,False无论先前的值是什么,您都只是将值保留下来。

So, simply change the value in savereportupon click of the button.

因此,只需savereport单击按钮即可更改值。

<script>
function change() {
  document.getElementsByName("savereport")[0].value = 'True';
}
</script>

<form name="test" method="post">
    <input type="text" name="savereport" value="False" />
</form>

<div align="center">
  <button type="button" value="false" id="check" onclick="change()" >Save</button>
</div>

回答by guest271314

The code at Question does not set the .valueof <input type="text" name="savereport" value="False" />element.

Question 中的代码未设置.valueof<input type="text" name="savereport" value="False" />元素。

You can set the valueof <input type="text" name="savereport" value="false" />element to "false"and evaluate boolean falseas a string to check for equality.

您可以设置value<input type="text" name="savereport" value="false" />元素"false"和评估布尔值false作为一个字符串来检查是否相等。

<form name="test" method="post">
  <input type="text" name="savereport" value="false" />
</form>

<div align="center">
  <button type="button" value="false" id="check" onclick="change()">Save</button>
  <script>
    var input = document.test.savereport;

    function change() {
      if (input.value === String(false)) {
        input.value = true;
        // document.test.submit();
      } else {
        input.value = false;
      }
      console.log(input.value);
    }
  </script>
</div>