如何使用jquery在按钮单击上调用函数?

时间:2018-06-18 20:51:07

标签: jquery

我有功能,我想查看是否在按钮点击时调用该功能。我能够使用js实现这一点,我看到警报,但我需要在jquery。

<button type="submit" onclick="sendEmail()" id="btnSubmit" formmethod="POST">SUBMIT</button>

$(document).ready(function(){
$("btnSubmit").click(function(){
    alert("Submit button is clicked");
});

});

3 个答案:

答案 0 :(得分:2)

尝试使用以下代码

$(document).ready(function(){
    $("#btnSubmit").click(function(){
        alert("Submit button is clicked");
    });
});

答案 1 :(得分:0)

你必须在Id&#34; btnSubmit&#34;的开头追加#符号。

`

let testString = "Guilcher, G.M., Harvey, M. & Hand, J.P.\nRi Liesner, Peter Tom Collins, Michael Richards\nManco-Johnson M, Santagostino E, Ljung R.";

const inputToArray = i => i
    .replace(/\./g, "")
    .replace(/[\n&]/g, ",")
    .replace(/ ?, ?/g, ",")
    .split(',');

const reducer = function(accumulator, value, index, array) {
    let pos = accumulator.length - 1;
    let names = value.split(' ');
    if(names.length > 1) {
        accumulator.push(names);
    } else {
        if(accumulator[pos].length > 1) accumulator[++pos] = [];
        accumulator[pos].push(value);
    }
    return accumulator.filter(n => n.length > 0);
};

console.log(inputToArray(testString).reduce(reducer, [[]]));

`

答案 2 :(得分:0)

使用click()功能的另一个选择是使用on()

$("#btnSubmit").on('click', function(){
    alert($(this));
})