【问题标题】:Pass 2 Functions Through One OnChange Event - With HREF on both Functions通过一个 OnChange 事件传递 2 个函数 - 在两个函数上都使用 HREF
【发布时间】:2019-07-06 14:09:23
【问题描述】:

我过去曾问过类似的问题,但能够通过按事件分隔功能来解决它​​。我需要能够在一个 Onchange 事件中传递 2 个 href 事件,因为它是一个下拉菜单,或者我需要能够将第二个函数绑定到另一个事件中。

这仅在插入 alert() 时有效。一旦我把 alert() 拿出来,它就不起作用了。我试图抑制警报,同时仍将其保留在代码中,并且工作正常。我不想要警报,但我想要结果。

HTML 在这里:

<select id="PartList" class="form-control form-control-lg ml-0" onChange="SelectMain();">

JavaScript 在这里

function sList() {
  var pl = document.getElementById("PartList");
  var value = pl.options[pl.selectedIndex].value;
  var text = pl.options[pl.selectedIndex].text;
  str = 'URL1 HERE='+ "'" + text + "'" ;
  //alert(value);
  //alert(text);
  window.location.href = str;
}
function SelectValue() {
  var pv = document.getElementById("PartList");
  var value = pv.options[pv.selectedIndex].value;
  str = 'URL2 HERE' + value ;
  alert(value);
  window.location.href = str;
}
function SelectMain() {
  sList();
  SelectValue();
}
function alert(message) { 
    console.info(message);
}

【问题讨论】:

    标签: javascript html function


    【解决方案1】:

    对于那些提出这个问题的人来说,这已经解决了。问题不在于 JavaScript,而是因为我发送命令的设备无法快速处理命令。我已将已解决的代码与故障排除技术结合在一起。

    function sList() {
      var pl = document.getElementById("PartList");
      var value = pl.options[pl.selectedIndex].value;
      var text = pl.options[pl.selectedIndex].text;
      str = 'URL1='+ "'" + text + "'" ;
      //str1 = 'http://google.com';
      //alert(value);
      //alert(text);
      window.location.href = str;
      //window.open(str1);
    }
    function SelectValue() {
      setTimeout(function(){
      var pv = document.getElementById("PartList");
      var value = pv.options[pv.selectedIndex].value;
      str = 'URL2=' + value ;
      //str1 = 'http://aol.com';
      //alert(value);
      window.location.href = str;
      //window.open(str1);
    },1000);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 2012-08-14
      • 2015-04-02
      • 2021-12-01
      • 2022-10-13
      • 2013-12-27
      • 1970-01-01
      • 2014-05-05
      相关资源
      最近更新 更多