2014-10-08 64 views
0

我有一个Bootstrap 3表单,在<button>元素上使用它的collapse()函数显示。 然后,我会,一旦表格打开,有这个相同的按钮成为窗体上的提交按钮。 但是,我不知道如何更改按钮的功能第二次按使用按钮有两个功能 - 显示内容,并提交

示例代码:

<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> 

jQuery函数准备代码:

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

这里是在动作切换一个的jsfiddle:http://jsfiddle.net/tzosozwv/

预先感谢。

回答

0

呼叫submit()form元素在你的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"); 
} 
0

只需添加一个提交

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

你好,这是所需的代码。请享用。

$(".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'); 
}