我想让第二个按钮单击事件仅在第一个按钮单击事件起作用后才起作用。在单击第一个按钮之前,第二个按钮的单击事件不得起作用。 并且还想要第一个按钮上的文本框用于过去的网址 意味着当观众没有将网址粘贴到文本框中并单击“确定”时 第二个按钮不起作用 喜欢 Nomigamer.com 在他的网站中使用
最佳答案
您可以通过创建标志来做到这一点。
flag默认值为 false,当点击第一个按钮时,flag为 true。
当点击第二个按钮时,它会检查标志是否启用,如果启用则可以处理第二个按钮。
举个例子:
var flag = false;
$("#firstButton").click(function(){
flag = true;
//Implement code for first button here
});
$("#secondButton").click(function(event){
if(flag === false) {
event.preventDefault();
}
//Implement code for second button here
});
第二个问题。我还是不明白你的意思。您能描述得更详细一些吗?
编辑
根据您的要求添加了完整代码
<html>
<head>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<input type="text" id="input1" value="12345">
<input type="text" id="input2">
<button id="button1">First button</button>
<button id="button2">Second button</button>
<script>
var flag = false;
//Because first button has id button1 so I use #button1 to handle event click
$("#button1").click(function(){
//Enable flag
flag = true;
// Because first input has id input1 so I use #input1 to get it element
// $("#input1").val() will get value of first input
var firstInputValue = $("#input1").val();
// Set value of first input is empty
$("#input1").val("");
// Set value of input 1 for input 2
$("#input2").val(firstInputValue);
});
$("#button2").click(function(event){
//If flag is disabled then exit function, Don't process anything.
if(flag === false) {
event.preventDefault();
return;
}
// If flag is enable then set value input1 = input2 and clear input2
var secondInputValue = $("#input2").val();
$("#input2").val("");
$("#input1").val(secondInputValue);
});
</script>
</body>
</html>
它有一些错误尚未修复。你的工作就是找到解决方案来做到这一点:D
关于javascript - 我想让第二个按钮单击事件仅在第一个按钮单击事件起作用后才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57527947/