简体   繁体   English

javascript检查是否已单击按钮

[英]javascript check if a button has been clicked

i have a problem with a function that is executing once the page is refreshed and onloaded , i want to execute on click event , so there is a button once the user has clicked the button the function should be called and run the code . 我对刷新和加载页面后正在执行的函数有问题,我想在click事件上执行,因此一旦用户单击按钮,就会出现一个按钮,应调用该函数并运行代码。 any ideas ? 有任何想法吗 ? this is my script 这是我的剧本

html html

<input type="submit" id="fff" value="Valider"  Onclick="validation(zero,one,two,three,four)" >

javascript javascript

function validation(f,a,b,c,d) {
    var str = '';
    var dataString = str;
    $.ajax( {
        type: 'GET',
        url: '/voir/valider/'+f+'/'+a+'/'+b+'/'+c+'/'+d+'/',
        data: dataString,

        success: function(data) {
            $('#shows2').html(data);
        }
    } );
    $('#fff').addClass('hide');
}

Looks like you might be getting some errors on your parameters, try using strings. 看起来您的参数可能会出现一些错误,请尝试使用字符串。 The browser probably thinks you have not defined variables of the same name. 浏览器可能认为您没有定义相同名称的变量。 If your function is being executed automatically it might be that some other code is attempting to call a generic validation function. 如果您的函数正在自动执行,则可能是其他一些代码正在尝试调用通用验证函数。 Try renaming it to Guns_Roses_Validation... 尝试将其重命名为Guns_Roses_Validation ...

 function Guns_Roses_Validation(f,a,b,c,d) { console.log("It works!") } 
 <input type="submit" id="fff" value="Valider" onclick="Guns_Roses_Validation('zero','one','two','three','four')" > 

Sounds like you want to only allow a single click to the button. 听起来您只希望单击一下按钮。

Add a prop ( __clicked , in this case) to the function that is set once and ignored if set. 将道具(在这种情况下为__clicked )添加到一次设置的函数中,如果设置则忽略。

 function validation(h,o,w,d,y) { if(!this.__clicked) { this.__clicked = true; document.body.appendChild(document.createTextNode("clicked")); } } 
 <input type="submit" id="fff" value="Valider" onclick='validation("zero","one","two","three","four")' > 

Also put quotes around the args passed in your click handler if they are string. 如果是字符串,还请在您的点击处理程序中传递的参数周围加上引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM