简体   繁体   English

为 1 个按钮集成 2 个功能,并在重新加载后保持输入字段禁用

[英]Integrate 2 functions for 1 button and keep input fields disabled after reload

A) I would like to have 2 different functions for 1 button. A) 我想为 1 个按钮提供 2 个不同的功能。 For first click function 1 should start, for second click function 2, for third click function 1, fourth click function 2... For first click function 1 should start, for second click function 2, for third click function 1, fourth click function 2...

For now I need 2 buttons, one disables and the other one enables the possibility to input data in a form field.现在我需要 2 个按钮,一个禁用,另一个启用在表单字段中输入数据的可能性。 The best would be to have both functions for 1 button (as explained above).最好是同时拥有 1 个按钮的两种功能(如上所述)。

Does anyone has an idea how to do that?有谁知道如何做到这一点?

B) All data get saved and can be reopened in the datamanagementsystem. B) 所有数据都被保存并且可以在数据管理系统中重新打开。 I would like that disabled fields stay disabled (after reopening the form again) for input.我希望禁用的字段保持禁用状态(再次重新打开表单后)以供输入。 Is there a possibility to do so?有没有可能这样做?

    <script>
function disable18() {
  document.getElementById("field1").disabled = true;
}
function enable18() {
    document.getElementById("field1").disabled = false;
}
</script>

My asnwer will solve problem A however problem B is a little more complicated.我的回答将解决问题 A,但问题 B 稍微复杂一些。 You will need a flag for this, for example this is your html您将需要一个标志,例如这是您的 html

<input type="text" id="field1">
<button id="toggler" class="btn btn-success" onclick="toggleNav()">Toggler</button>

in your js start off with creating your flag, let's set it to false在你的 js 中从创建你的标志开始,让我们将它设置为 false

var nav = false;

When your 1st function is called change your flag to true当您的第一个 function 被调用时,将您的标志更改为 true

function disable18() {
  document.getElementById("field1").disabled = true;
  nav = true;
}

Now for the second function we will set it back to false现在对于第二个 function 我们将其设置回 false

function enable18() {
  document.getElementById("field1").disabled = false;
  nav = false;
}

Now we create the function that toogles between the 2 of them现在我们创建 function 在它们两者之间切换

function toggleNav() {
  if(nav==false){
    disable18();
  } else {
    enable18();
  }
}

After that, all that's left is make sure your toggleNav() function in the onclick() in your button.之后,剩下的就是确保您的按钮 onclick() 中的 toggleNav() function 。 Now for problem BI have more questions than answers.现在对于问题,BI 的问题多于答案。 Need more details about how do you want to do achieve that需要更多关于你想如何做到这一点的细节

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

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