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

我正在尝试调用添加在标头中的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>
6个解决方案
34 votes

您正在尝试在加载元素之前附加事件侦听器功能。 将2996692027393493442816放置在onload事件侦听器函数中。 在此函数内调用2996692027393493442818,因为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.  
    }
}

JSFiddle

George answered 2020-08-04T15:21:13Z
10 votes

您可以使用addEventListener添加所需数量的侦听器。

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

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

加载dom时可以调用它而不是移动脚本标签。 然后,您应该将代码放入

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

jonasnas answered 2020-08-04T15:21:47Z
2 votes

在您执行功能之前,我已将您的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>
JORDANO answered 2020-08-04T15:22:09Z
1 votes

内联代码比其他代码具有更高的优先级。 要调用其他函数func(),请从f1()进行调用。

在函数内部,添加一行,

function fun () {
// Your code here
}

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

fun ();

    }

重写整个代码,

 <!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>
Arunkumar Srisailapathi answered 2020-08-04T15:22:38Z
1 votes

使用onclick属性或将函数应用于JS onclick属性将清除中的onclick初始化。

您需要做的是在按钮上添加点击事件。为此,您需要addEventListener和attachEvent(即)方法。

<!DOCTYPE html>
<html>
<head>
    <script>
        function addEvent(obj, event, func) {
            if (obj.addEventListener) {
                obj.addEventListener(event, func, false);
                return true;
            } else if (obj.attachEvent) {
                obj.attachEvent('on' + event, func);
            } else {
                var f = obj['on' + event];
                obj['on' + event] = typeof f === 'function' ? function() {
                    f();
                    func();
                } : func
            }
        }

        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>

    <script>
        addEvent(document.getElementById('Save'), 'click', function() {
            alert('hello');
        });
    </script>
</body>
</html>
eltyweb answered 2020-08-04T15:23:03Z
-4 votes
function validate() {

    if( document.myForm.phonenumber.value == "" ) {
        alert( "Please provide your phonenumber!" );
        phonenumber.focus;
        return false;
    }
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value))  {  
        return (true)  
    }  
    alert("You have entered an invalid email address!");
    return (false);
}
harsh answered 2020-08-04T15:27:02Z
translate from https://stackoverflow.com:/questions/21477717/how-to-call-a-js-function-using-onclick-event