Javascript 使用按钮可实现两个功能:显示内容和提交

Javascript 使用按钮可实现两个功能:显示内容和提交,javascript,jquery,html,twitter-bootstrap,Javascript,Jquery,Html,Twitter Bootstrap,我有一个bootstrap3表单,它在一个元素上使用它的折叠函数显示。 然后,一旦表单打开,我会让这个按钮成为表单上的提交按钮。 然而,我不知道如何在第二次按下时更改按钮的功能 示例代码: <form class="collapse" id="1234"> <div class="form-group"> <input class="form-control" type="text" placeholder="Name"> &l

我有一个bootstrap3表单,它在一个元素上使用它的折叠函数显示。 然后,一旦表单打开,我会让这个按钮成为表单上的提交按钮。 然而,我不知道如何在第二次按下时更改按钮的功能

示例代码:

<form class="collapse" id="1234">
    <div class="form-group">
        <input class="form-control" type="text" placeholder="Name">
    </div>
    <div class="form-group">
        <input class="form-control" type="email" placeholder="Email">
    </div>
</form>
<button class="btn btn-default btn-block" data-toggle="collapse" data-target="#1234">Open Form</button>
下面是一个使用切换的JSFIDLE:

提前感谢。

在您的if声明中调用表单元素提交:

if ($.trim($(this).text()) == "Open Form") {
    $(this).text("Submit Form");
} else {
    // Presumably this is where you want your form submitted...
    $('form#1234').submit();
    $(this).text("Open Form");
}
只需添加一个提交

$(".btn-block").click(function () {
    if ($.trim($(this).text()) == "Open Form") {
        $(this).text("Submit Form");
    } else {
        $('#1234').submit();
        $(this).text("Open Form");
    }
});

嗨,这是所需的代码。享受

$(".btn-block").click(function () {
    if ($.trim($(this).text()) == "Open Form") {
    $(this).text("Submit Form");
    $(this).on("click",callfunction);
} else {
    $(this).text("Open Form");
    $(this).off("click",callfunction);
}
});

function callfunction(){
    alert('ak');
}
$(".btn-block").click(function () {
    if ($.trim($(this).text()) == "Open Form") {
    $(this).text("Submit Form");
    $(this).on("click",callfunction);
} else {
    $(this).text("Open Form");
    $(this).off("click",callfunction);
}
});

function callfunction(){
    alert('ak');
}