javascript - 从代码中的其他地方调用 JQuery 事件作为函数...?

标签 javascript html function jquery

好吧,非常简单的 JQuery 问题,我正在努力寻找答案:

我有一个在单击按钮时调用的 JQuery 事件:

$(document).ready(function(){
        resetForms('reservation');
        $('#form-reservation').submit(function(event){ 
            event.preventDefault();  //the page will no longer refresh on form submit.
            var resCheck = $(this).find('input[class="reservationid"]').val(); //now we have the reservation ID, let's perform our check.
            document.cancel_res.cancel_agree.checked = false;
            //document.cancel_res.cancel_agree.disabled = false;
            document.cancel_res.cancel_button.disabled=true;
            document.form_reservation.search_button.value="Searching...";
            document.form_reservation.search_button.disabled=true;
            $.ajax({ 
                url: 'inc/searchres.php', 
                type: 'POST',
                data: 'resid='+resCheck, 
                success: function(data){  //data is all the info being returned from the php file 
                    resetForms('reservation');  //clear forms
                    document.form_reservation.search_button.value="Search Reservation";
                    document.form_reservation.search_button.disabled=false;
                    $('#reservation-id').val(resCheck);  //add read ID back into text box
                    var jsonData = $.parseJSON(data);
                    //BLAH BLAH BLAH BLAH
                }
            });
        });
    });

该函数完美运行...但是,是否可以在不使用提交事件的情况下调用该函数?我试图在 $('#form-reservation').submit(function(event){ 调用之后取出所有内容并将其放在一个单独的函数中,然后从提交事件中调用该函数. 但是,无论出于何种原因,这都失败了。

基本上,我希望提交事件仍然触发这个函数,但我也希望在其他情况下能够调用整个函数。提前致谢!

最佳答案

其实很简单:

var MyFunc = function(event){ 
            typeof event !== 'undefined' ? event.preventDefault() : false;  //the page will no longer refresh on form submit.
            var resCheck = $(this).find('input[class="reservationid"]').val(); //now we have the reservation ID, let's perform our check.
            document.cancel_res.cancel_agree.checked = false;
            //document.cancel_res.cancel_agree.disabled = false;
            document.cancel_res.cancel_button.disabled=true;
            document.form_reservation.search_button.value="Searching...";
            document.form_reservation.search_button.disabled=true;
            $.ajax({ 
                url: 'inc/searchres.php', 
                type: 'POST',
                data: 'resid='+resCheck, 
                success: function(data){  //data is all the info being returned from the php file 
                    resetForms('reservation');  //clear forms
                    document.form_reservation.search_button.value="Search Reservation";
                    document.form_reservation.search_button.disabled=false;
                    $('#reservation-id').val(resCheck);  //add read ID back into text box
                    var jsonData = $.parseJSON(data);
                    //BLAH BLAH BLAH BLAH
                }
            }

$(document).ready(function(){
        resetForms('reservation');
        $('#form-reservation').submit(MyFunc); //this calls on submit
    });

//this calls without need of a submit
MyFunc();

关于javascript - 从代码中的其他地方调用 JQuery 事件作为函数...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10358102/

相关文章:

javascript - 多个 <script> 标签,每个标签都有自己的作用域

javascript - 如何缩短 typescript 中的界面名称?

javascript - 如何添加 React-Native 原生模块设置依赖?

javascript - 如何检查字符串中的每个数字?

html - 如何在 <IE8 中嵌入 block ?

javascript - 通过计时器在照片库上切换聚焦图片

php - 如何在 .htaccess 中包含 php 文件?

python - 如何在不破坏标签的情况下截断 html?

javascript - javascript变量/函数的首字母是什么意思

javascript - Javascript 游戏 - 添加更多问题