Html 如何在选择标签的更改时调用 JavaScript 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6159837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:48:28  来源:igfitidea点击:

How to call a JavaScript function onchange of a select tag?

htmlselectonchange

提问by laxmi

Below is my JavaScript:

下面是我的 JavaScript:

<script language="JavaScript">
function checkit()
{  
var state=document.frm.getElementById(stateS)
if (state="Please select a state")
    {
    document.frm.Month.disable = true;
    }
else
    {
    document.frm.Month.enable = true;
    }   
}</script>

and I call checkit()function in it from below <select>tag:

checkit()从下面的<select>标签调用函数:

<td align="left"><g:select name="Month" from="${month.values()}" optionValue="${Month}" onChange=checkit()/></td>
 </tr> 

I have a select tag for state and until I select any state the month select box should be disabled. Can anyone tell me where I went wrong?

我有一个状态选择标签,在我选择任何状态之前,应该禁用月份选择框。谁能告诉我我哪里出错了?

回答by Hussam Sibai

actually there is a " missing there, it should be

实际上有一个“失踪”,应该是

onChange="checkit();"

回答by Jonas Elfstr?m

Change the onchangeto onChange="checkit(this);and then something like the below in checkit

更改onchangeonChange="checkit(this);,然后类似于下面的checkit

function checkit(selectObj)
{ 
  var idx = selectObj.selectedIndex;
  document.frm.Month.disabled = idx == 0;
}

回答by Alonso Mulgado

by id jquery-1.9.1

通过 id jquery-1.9.1

$('#selectBox').on('change', function() 
{
    value= this.value;
    if(value==0)
      {
        //code
      }
    else if(value==1)
      {
         //code
      }
     ...
     else
      {
         //code
      }
  });

回答by Okaba Mac

document.querySelector("select").onchange = function() {myFunction()};

document.querySelector("select").onchange = function() {myFunction()};

回答by letsjump

I needed to put its javascript function beforethe input tag is generated.
EG.outside jourJSframework(document).ready function or before the <\head> closing tag of your html document.

我需要生成输入标签之前放置它的 javascript 函数。
EG.outside jourJSframework(document).ready 函数或在您的 html 文档的 <\head> 结束标记之前。