如何从下拉菜单中选择并调用javascript函数

sad*_*di 19 html javascript

我有一个下拉,有很多选择.我希望当我选择任何选项时,它会通过JavaScript调用一个函数.

我使用的代码就在这里

<select name="aa" onchange="report(this.value)"> <--- this is function in .js
<option value="daily">daily</option>
<option value="monthly">monthly</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我想在每天选择时调用函数(每日),反之亦然.

function report(daily)<-- js function {  
  loadXMLDoc('script/d_report.php','responseTag');
  document.getElementById('responseTag').style.visibility='visible';
  document.getElementById('list_report').style.visibility='hidden';
  document.getElementById('formTag').style.visibility='hidden'; 
} 
function report(monthly) {
  document.getElementById('responseTag').style.visibility='visible';
  loadXMLDoc('script/m_report.php','responseTag');
  document.getElementById('list_report').style.visibility='hidden';
  document.getElementById('formTag').style.visibility='hidden';
}
Run Code Online (Sandbox Code Playgroud)

mpl*_*jan 28

<select name="aa" onchange="report(this.value)"> 
  <option value="">Please select</option>
  <option value="daily">daily</option>
  <option value="monthly">monthly</option>
</select>
Run Code Online (Sandbox Code Playgroud)

运用

function report(period) {
  if (period=="") return; // please select - possibly you want something else here

  var report = "script/"+((period == "daily")?"d":"m")+"_report.php";
  loadXMLDoc(report,'responseTag');
  document.getElementById('responseTag').style.visibility='visible';
  document.getElementById('list_report').style.visibility='hidden';
  document.getElementById('formTag').style.visibility='hidden'; 
} 
Run Code Online (Sandbox Code Playgroud)

不引人注目的版本:

<select id="aa" name="aa"> 
  <option value="">Please select</option>
  <option value="daily">daily</option>
  <option value="monthly">monthly</option>
</select>
Run Code Online (Sandbox Code Playgroud)

运用

window.onload=function() {
  document.getElementById("aa").onchange=function() {
    var period = this.value;
    if (period=="") return; // please select - possibly you want something else here

    var report = "script/"+((period == "daily")?"d":"m")+"_report.php";
    loadXMLDoc(report,'responseTag');
    document.getElementById('responseTag').style.visibility='visible';
    document.getElementById('list_report').style.visibility='hidden';
    document.getElementById('formTag').style.visibility='hidden'; 
  } 
}
Run Code Online (Sandbox Code Playgroud)

jQuery版本 - 与ID一样选择

$(function() {
  $("#aa").on("change",function() {
    var period = this.value;
    if (period=="") return; // please select - possibly you want something else here

    var report = "script/"+((period == "daily")?"d":"m")+"_report.php";
    loadXMLDoc(report,'responseTag');
    $('#responseTag').show();
    $('#list_report').hide();
    $('#formTag').hide(); 
  }); 
});
Run Code Online (Sandbox Code Playgroud)