简体   繁体   English

使用HTML下拉菜单调用Javascript函数

[英]Calling Javascript functions with HTML dropdowns

I've been trying to call functions from a drop-down menu, but for some reason only shift(CPL) seems be called. 我一直试图从下拉菜单中调用函数,但是由于某种原因,似乎只调用了shift(CPL)。 I have verified that the functions themselves are working, and have redacted those for easier reading. 我已经验证了这些函数本身是否可以正常工作,并且已经对它们进行了编辑以便于阅读。 I'm very new to Javascript and may have made a fairly basic error, apologies for that and thanks in advance! 我是Java的新手,可能犯了一个非常基本的错误,对此深表歉意,并在此先感谢!

HTML: HTML:

<select onchange="shift(this.value);">
  <option value="CPM">CPM</option>
  <option value="CPC">CPC</option>
  <option value="CPL">CPL/A</option>
</select>

Javascript: 使用Javascript:

function shift(CPM) {

}

function shift(CPC) {

}

function shift(CPL) {

}


</script>

I think you didn't really understand how functions work in programming languages, i recommend you to study a little more and follow guides before trying to create something new. 我认为您不太了解函数在编程语言中的工作方式,我建议您在尝试创建新内容之前,多学习一点并遵循指南。

Basically, you don't have to create 3 different shift function: one is more then enough: only the parameter will change. 基本上,您不必创建3个不同的shift函数:一个函数就足够了:只需更改参数即可。 try this code: 试试这个代码:

function shift (genericParameter) {
  if (genericParameter == 'CPM') {
     //CPM
  } else if (genericParameter == 'CPC') {
     //CPC
  } else if (genericParameter == 'CPL') {
    //CPL
  }
}

For how to do it in jquery : 有关如何在jquery做到这一点:

DEMO DEMO

js code: js代码:

$(document).ready(function(){

  $('select').on('change', function() {
   shift(this.value);
  });

});

function shift(yourValue) {
    alert(yourValue) //Implement what you need here. 
}

//or maybe you prefer this:

function shift(yourValue) {
    switch(yourValue) {
        case 'CPM':
            //code block
            break;
        case 'CPC':
            //code block
            break;
        case 'CPL':
            //code block
            break;
        default:
            default code block
    }
}

hope it's helps. 希望对您有所帮助。

Try creating an object having properties of select values , at selection do stuff corresponding to property name of object 尝试创建一个具有select值属性的对象,在选择时会做与对象的属性名称相对应的操作

 var selected = { "CPM":"option 1", "CPC": function(v) { return v.toLowerCase() }, "CPL": "option 3" } function shift(val) { var res = typeof selected[val] === "function" ? selected[val](val) : selected[val]; alert(res) } 
 CPM <select onchange="shift(this.value);"> <option value="CPM">CPM</option> <option value="CPC">CPC</option> <option value="CPL">CPL/A</option> </select> 

i think your missing just syntax , this can help you.. 我认为您缺少的只是语法,可以为您提供帮助。

enter code here

<select id="selectDrop" onchange="copy();">
    <option value="">Default:</option>
    <option value="one" >one</option>
    <option value="two" >two</option>
    <option value="three" >three</option>
  </select>

JavaScript :- JavaScript的:-

  function copy(){
     alert(document.getElementById("selectDrop").value);
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM