如何在细箭头函数中包含多个语句?

时间:2021-02-12 19:39:43

This works:

<script>
    $(modal).on("click", 'input[type="submit"]', (e) ->
        modal.modal('hide'))
</script>

But how do I to additionally include an alert("Success!") in the above, so that it both hides the modal and then displays an alert when submit is clicked?

但是我如何在上面另外包含一个警报(“成功!”),以便它隐藏模态,然后在单击提交时显示警告?

1 个解决方案

#1


0  

You need braces around to signify a new block that allows for multiple statements. Without it, the function can only execute one line. Try this:

您需要大括号来表示允许多个语句的新块。没有它,该功能只能执行一行。试试这个:

$(modal).on("click", 'input[type="submit"]', (e) -> {
    modal.modal('hide');
    alert("Success!");
});

This uses some braces to create a function that can execute more than one line. Your previous example is similar to a shorthand if statement like so:

这使用一些大括号来创建一个可以执行多行的函数。您之前的示例类似于简写if if语句,如下所示:

if(5 > 3) 
    console.log("Hello!");

It's great for short blocks of code, but you need braces if you want more than one line of code:

它适用于短代码块,但如果您需要多行代码,则需要大括号:

if(5 > 3) {
    console.log("Hello!");
    alert("Apples!");
}

#1


0  

You need braces around to signify a new block that allows for multiple statements. Without it, the function can only execute one line. Try this:

您需要大括号来表示允许多个语句的新块。没有它,该功能只能执行一行。试试这个:

$(modal).on("click", 'input[type="submit"]', (e) -> {
    modal.modal('hide');
    alert("Success!");
});

This uses some braces to create a function that can execute more than one line. Your previous example is similar to a shorthand if statement like so:

这使用一些大括号来创建一个可以执行多行的函数。您之前的示例类似于简写if if语句,如下所示:

if(5 > 3) 
    console.log("Hello!");

It's great for short blocks of code, but you need braces if you want more than one line of code:

它适用于短代码块,但如果您需要多行代码,则需要大括号:

if(5 > 3) {
    console.log("Hello!");
    alert("Apples!");
}