开发者

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

开发者 https://www.devze.com 2023-03-09 08:52 出处:网络
Below is my JavaScript: <script language=\"JavaScript\"> function checkit() { var state=document.frm.getElementById(stateS)

Below is my 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:

<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?


actually there is a " missing there, it should be

onChange="checkit();"


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

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


by id jquery-1.9.1

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消