Javascript Function Parameters Not Working

泪湿孤枕 提交于 2020-01-05 05:50:11

问题


Every site I've read says a function would just take a parameter if you declare it, however I can't get it to work here.

Working like this:

    <script type='text/javascript'>
    function trackSubmit() { 
        setTimeout(function(){
            ga('send', 'event', 'category', 'action', 'link', 4);
        }, 100);
     }
</script>

And with:

<form onsubmit="trackSubmit()">

However, if I try something like this, it doesn't work.

    <script type='text/javascript'>
    function trackSubmit(category, action, link) { 
        setTimeout(function(){
            ga('send', 'event', category, action, link, 4);
        }, 100);
     }
</script>

<form onsubmit="trackSubmit(testcategory, testaction, testlink)">

What can I do to fix this? Or should I just have multiple functions like trackSubmit1, trackSubmit2, etc? Though that wouldn't be very convenient.

Thank you for any help.


回答1:


you need to use qoutation <form onsubmit="trackSubmit('testcategory', 'testaction', 'testlink')"> if there are variable then need to concatenate like \''+testcategory+'\' , ...



来源:https://stackoverflow.com/questions/24231150/javascript-function-parameters-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!