dongyinzheng6572 2017-05-20 04:43
浏览 60
已采纳

如何在选择按钮组中的特定按钮时分配不同的URL链接?

I want to create a group of buttons that when clicked will return different download link to the user email. I am trying to do it with javascript switch but I not sure how to return the download link so that the download link can be retrieved using PHP get to send in the email.

<div class="row">
  <div class="col-md-3">
    <div class="text-center">
      <i class="fa fa-file" style="font-size:48px; color:#00ffff;"></i>
      <span><h6>User Manual:Accounting</h6></span>
      <button class="downloadButton" id="401" type="button" data-toggle="modal" data-target="#downloadModal" style="border-radius:5px; outline:none; background-color:Transparent;">
      <i class="fa fa-download" style="font-size:24px; color:#737373;"></i>
      </button>
   </div>
 </div>

 <script>
function linkFunction(){
    var buttonClass = document.getElementsByClassName["download-button"];
    var buttonId = buttonId.attr('id');
    var link;
  • 写回答

1条回答 默认 最新

  • donglu1913 2017-05-20 06:19
    关注

    Is this maybe what you're looking for? Hope it helps!

    <html>
    <head>
    <script>
    window.onload = doThis;
    
    /* This one would work well and allow you to pass arguments */
    function doThis() {
      var buttonClass = document.getElementById("download_button");
      var buttonId = buttonClass.id;
      var buttonName = buttonClass.name;
      var buttonLinkId = buttonClass.getAttribute('data-linkId');
    
      buttonClass.addEventListener('click', buttonClickedCaller);
    
      function buttonClickedCaller(){
        buttonClicked(buttonLinkId);
      }
    
      function buttonClicked(foo) {
        console.log('http://localhost/?' + foo);
        window.location = 'http://localhost/?' + foo;
        // window.open('http://localhost/?'+foo);
      }
    }
    
    /*
    // here's a simpler example
    function doThis() {
      var buttonClass = document.getElementById("download_button");
      var buttonId = buttonClass.id;
      var buttonName = buttonClass.name;
      var buttonLinkId = buttonClass.getAttribute('data-linkId');
    
      buttonClass.addEventListener('click', buttonClicked);
    
      function buttonClicked() {
        console.log('http://' + buttonLinkId);
      }
    }
    
    //  careful though, this won't work.
    function doThis() {
      var buttonClass = document.getElementById("download_button");
      var buttonId = buttonClass.id;
      var buttonName = buttonClass.name;
      var buttonLinkId = buttonClass.getAttribute('data-linkId');
      // Passing a Variable in Below Here
      buttonClass.addEventListener('click', buttonClicked(buttonLinkId));
      // Will Break Here & It's executed onload, but the above one isn't
      function buttonClicked(foo) {
        console.log('http://' + foo);
      }
    }
    */
    
    
    </script>
    </head>
    <body>
    <div class="row">
      <div class="col-md-3">
        <div class="text-center">
          <span><h6>User Manual:Accounting</h6></span>
          <button id="download_button" data-linkId="401">
            Click Here
          </button>
        </div>
      </div>
    </body>
    <html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败