【问题标题】:How to call the same function more than one time using parameters in javascript? [closed]如何在javascript中使用参数多次调用同一个函数? [关闭]
【发布时间】:2020-05-07 13:13:10
【问题描述】:
<script>
function txtchk(as, tb) { 
//as is gonna be an asterisk
//tb is a an input type text
if (document.getElementById(tb).value == "") {
document.getElementById(as).style.display = "block";
}
else {
document.getElementById(as).style.display = "none";
}
}
</script>

所以当 tbox 为空时,显示星号,否则不显示。 if语句是正确的,但是参数怎么写呢?

<div class="b1">
<h>Book Name:</h>
<h class="astyle" id="asterisk1">*</h>
<div class="b2">
<input type="text" id="tbox1" oninput="txtchk(asterisk1, tbox1)">
//is using on input wrong?
</div>

在这里,我希望使用相同的函数但不同的 id 发生同样的事情。

<div class="b1">
<h class="b1c">Description:</h>
<h class="astyle" id="asterisk2">*</h>
<div class="b2">
<textarea class="tarea" id="tbox2" oninput="txtchk(asterisk2, tbox2)">
</textarea>
</div>
</div>

【问题讨论】:

    标签: javascript html function parameters


    【解决方案1】:

    您的参数必须是字符串。像这样改变你的 oninput

    oninput="txtchk('asterisk2', 'tbox2')"
    

    【讨论】:

      猜你喜欢
      • 2017-01-19
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      相关资源
      最近更新 更多