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

问题描述:

我有一个下拉菜单,它有很多选项。我希望当我选择任何 选项时,它会通过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> 

我想,当我选择每天则函数(每日)被调用 ,反之亦然。

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'; 
} 
+0

哪里是你的代码相同的选择?功能在哪里? – 2011-03-16 09:44:26

+0

其在HTML代码 – sadi 2011-03-16 09:46:35

+0

每日 每月 – sadi 2011-03-16 09:47:21

<select name="aa" onchange="report(this.value)"> 
    <option value="">Please select</option> 
    <option value="daily">daily</option> 
    <option value="monthly">monthly</option> 
</select> 

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'; 
} 

不显眼的版本:使用

<select id="aa" name="aa"> 
    <option value="">Please select</option> 
    <option value="daily">daily</option> 
    <option value="monthly">monthly</option> 
</select> 

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'; 
    } 
} 

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(); 
    }); 
}); 
+0

感谢它现在的工作 – sadi 2011-03-17 18:52:40

<script type="text/javascript"> 
function report(func) 
{ 
    func(); 
} 

function daily() 
{ 
    alert('daily'); 
} 

function monthly() 
{ 
    alert('monthly'); 
} 
</script> 
+0

在html中它的权利????? – sadi 2011-03-16 09:59:08

+0

区别在于要显示哪个报表。为什么有多个功能,当你需要的只是一个简单的选择 – mplungjan 2011-03-16 10:03:26

问候 如果我得到你的权利,你需要一个JavaScript函数,用做

function report(v) { 
//To Do 
    switch(v) { 
    case "daily": 
     //Do something 
     break; 
    case "monthly": 
     //Do somthing 
     break; 
    } 
    } 

问候

+0

函数(每日) – sadi 2011-03-16 09:53:18

+0

我没有得到你做你的意思是你需要每天输入作为枚举不是在“每日”? – Marwan 2011-03-16 11:15:05