如果单击特定按钮,则显示消息

时间:2019-09-19 15:59:04

标签: javascript jquery html

我一般来说是JavaScript和网络开发的新手,但我想做的是:创建不同的表单,并根据单击的按钮,运行特定的代码(目前仅显示内容)。

这就是我所做的

<script>
    function init() {

      //this.onclick = showAlert;
      var p1 = document.getElementById('btn-hello');
      var p2 = document.getElementById ('btn-web');
      var p3 = document.getElementById('btn-prive');

      p1.onclick = showAlert('msghello.txt');
      p2.onclick = showAlert('msgweb.txt');
      p3.onclick = showAlert('msgprive.txt');

    function showAlert(file) {
      // Requiring fs module in which 
      // readFile function is defined.

      $.get(file, function(data) { alert(data)}, 'text');

  }
    </script>

enter image description here 其他按钮都是一样的,只是更改了“ id”。

问题在于,无论我单击哪个按钮,它都会为每个按钮运行代码。 我也尝试使用此参数,但仍然无法正常工作。

0 个答案:

没有答案
相关问题