如何使用OnClick事件调用JS函数

Vin*_*rma 44 html javascript forms validation html-validation

我试图调用我在头文件中添加的JS函数.请在下面找到显示我的问题场景的代码.注意:我无法访问应用程序中的正文.每次我点击元素id="Save"时只会调用f1()但不是fun().我怎么能打电话给我fun()?请帮忙.

  <!DOCTYPE html>
  <html>
  <head>

  <script>

   document.getElementById("Save").onclick = function fun()
    {
     alert("hello");
     //validation code to see State field is mandatory.  
    }   

    function f1()
    {
       alert("f1 called");
       //form validation that recalls the page showing with supplied inputs.    
    }

  </script>
  </head>
  <body>
  <form name="form1" id="form1" method="post">
            State: 
            <select id="state ID">
               <option></option>
               <option value="ap">ap</option>
               <option value="bp">bp</option>
            </select>
   </form>

   <table><tr><td id="Save" onclick="f1()">click</td></tr></table>

   </body>
   </html>
Run Code Online (Sandbox Code Playgroud)

Geo*_*rge 35

您正在尝试在加载元素之前附加事件侦听器函数.将fun()内部onload事件侦听器的功能.调用f1()这个函数中,作为onclick属性将被忽略.

function f1() {
    alert("f1 called");
    //form validation that recalls the page showing with supplied inputs.    
}
window.onload = function() {
    document.getElementById("Save").onclick = function fun() {
        alert("hello");
        f1();
        //validation code to see State field is mandatory.  
    }
}
Run Code Online (Sandbox Code Playgroud)

的jsfiddle


jon*_*nas 10

您可以使用addEventListener添加任意数量的侦听器.

  document.getElementById("Save").addEventListener('click',function ()
    {
     alert("hello");
     //validation code to see State field is mandatory.  
    }  ); 
Run Code Online (Sandbox Code Playgroud)

还要script在元素后面添加标记,以确保Save在脚本运行时加载元素

而不是移动脚本标记,您可以在加载dom时调用它.然后你应该把你的代码放在

document.addEventListener('DOMContentLoaded', function() {
    document.getElementById("Save").addEventListener('click',function ()
    {
     alert("hello");
     //validation code to see State field is mandatory.  
    }  ); 
});
Run Code Online (Sandbox Code Playgroud)


JOR*_*ANO 6

document.getElementById("Save").onclick =在你的功能之前删除了你,因为它是一个已经在你的按钮上被调用的事件。我还必须通过 onclick 事件分别调用这两个函数。

     <!DOCTYPE html>
      <html>
      <head>
      <script>
       function fun()
        {
         alert("hello");
         //validation code to see State field is mandatory.  
        }   
        function f1()
        {
          alert("f1 called");
           //form validation that recalls the page showing with supplied inputs.    
        }
      </script>
      </head>
      <body>
      <form name="form1" id="form1" method="post">
                State: 
                <select id="state ID">
                   <option></option>
                   <option value="ap">ap</option>
                   <option value="bp">bp</option>
                </select>
       </form>

       <table><tr><td id="Save" onclick="f1(); fun();">click</td></tr></table>

   </body>
   </html>
Run Code Online (Sandbox Code Playgroud)


Aru*_*thi 0

内联代码的优先级高于<head>链接文件中的代码。要调用其他函数 func(),请从 f1() 调用它。

在你的函数中,添加一行,

function fun () {
// Your code here
}

function f1()
    {
       alert("f1 called");
       //form validation that recalls the page showing with supplied inputs.    

fun ();

    }
Run Code Online (Sandbox Code Playgroud)

重写你的整个代码,

 <!DOCTYPE html>
    <html>
      <head>
    
      <script>
    
       function fun()
        {
         alert("hello");
         //validation code to see State field is mandatory.  
        }   
    
        function f1()
        {
           alert("f1 called");
           //form validation that recalls the page showing with supplied inputs.   
           fun (); 
        }
    
      </script>
      </head>
      <body>
       <form name="form1" id="form1" method="post">
    
         State: <select id="state ID">
                   <option></option>
                   <option value="ap">ap</option>
                   <option value="bp">bp</option>
                </select>
       </form>
       <table><tr><td id="Save" onclick="f1()">click</td></tr></table>
    
      </body>
</html>
Run Code Online (Sandbox Code Playgroud)