将参数传递到同一按钮并在功能中检查参数是否条件

发布于 2025-02-10 20:46:06 字数 1007 浏览 1 评论 0原文

我有2个使用相同功能的按钮,我需要能够识别我按下哪个按钮。

在按钮1上,我将onClick =“ myFunction(1)”和按钮2我放置myFunction(2)然后在myFunction(a)上 如果我使用条件a = 1它将无法执行。

<!DOCTYPE html>
<html>
<body>
    
<!-- CSR -->
<label onclick="myFunction(a.value=1)">CSR</label>
<input type="text" value="Hello World" id="CSRNm"><br>
    
<!-- Acct# -->
<label onclick="myFunction(a.value=2)">Acct#</label>
<input type="text" value="Hello Other World" id="AccNo">
    
<script>
function myFunction(a) {  
  if (a.value=1){
    navigator.clipboard.writeText(CSRNm.value);
    /* Alert the copied text */
    alert("Copied the text: " + a.value);
  } else if (a.value=2){
    navigator.clipboard.writeText(AccNo.value);
    /* Alert the copied text */
    alert("Copied the text: " + AccNo.value);
  }  
}
</script>
    
</body>
</html>

I have 2 buttons that use the same function, I need to be able to identify which button I pressed.

On button 1 I put onclick="myFunction(1)" and on button 2 I put myFunction(2) then on myFunction(a) if I use if condition a=1 it won't execute.

<!DOCTYPE html>
<html>
<body>
    
<!-- CSR -->
<label onclick="myFunction(a.value=1)">CSR</label>
<input type="text" value="Hello World" id="CSRNm"><br>
    
<!-- Acct# -->
<label onclick="myFunction(a.value=2)">Acct#</label>
<input type="text" value="Hello Other World" id="AccNo">
    
<script>
function myFunction(a) {  
  if (a.value=1){
    navigator.clipboard.writeText(CSRNm.value);
    /* Alert the copied text */
    alert("Copied the text: " + a.value);
  } else if (a.value=2){
    navigator.clipboard.writeText(AccNo.value);
    /* Alert the copied text */
    alert("Copied the text: " + AccNo.value);
  }  
}
</script>
    
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2025-02-17 20:46:08

您为什么通过A.Value只需以a的方式传递:

<button id="btn1" onclick="myFunction(a=1)">one</button>
<button id="btn2" onclick="myFunction(a=2)">two</button>
const myFunction = (a) => {
    if (a === 1) {
        console.log("a");
    }
    if (a === 2) {
        console.log("b");
    }
};

Why are you passing a.value just pass it as a as follows:

<button id="btn1" onclick="myFunction(a=1)">one</button>
<button id="btn2" onclick="myFunction(a=2)">two</button>
const myFunction = (a) => {
    if (a === 1) {
        console.log("a");
    }
    if (a === 2) {
        console.log("b");
    }
};

孤者何惧 2025-02-17 20:46:08

而不是:

myFunction(a.value=1)

您可以使用以下内容:

myFunction({value: 1})

myFunction 的函数声明已将参数称为a

因此,当您传递对象文字{值:1}作为函数参数时,执行函数将返回1当询问a.Value 。

Instead of:

myFunction(a.value=1)

you can use this:

myFunction({value: 1})

The function declaration for myFunction already refers to the parameter as a.

So, when you pass the object literal {value: 1} as the function parameter, the executing function will return 1 when asked for a.value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文