将参数传递到同一按钮并在功能中检查参数是否条件
我有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您为什么通过
A.Value
只需以a
的方式传递:Why are you passing
a.value
just pass it asa
as follows:而不是:
您可以使用以下内容:
myFunction
的函数声明已将参数称为a
。因此,当您传递对象文字
{值:1}
作为函数参数时,执行函数将返回1
当询问a.Value 。
Instead of:
you can use this:
The function declaration for
myFunction
already refers to the parameter asa
.So, when you pass the object literal
{value: 1}
as the function parameter, the executing function will return1
when asked fora.value
.