2016-04-06 88 views
0

這是Featherlight hide div on Close在羽之燈箱呼叫按鈕

我已經成功地解決了打開收藏夾問題的後續問題,但我已經添加了一個提交按鈕,我想呼籲(「提交」,函數)點擊時。

jQuery(document).ready(function() {  
//Triggle when Preview Button is Clicked. 
jQuery('.OpenLightBox').off('click').on('click', function(e) { 

    var pa_firstname= jQuery('input[name="pa-name_first"]').val(); 
    var pa_lastname= jQuery('input[name="pa-name_last"]').val(); 
    if (pa_firstname == null || pa_firstname == '') { 
     alert('Cannot be empty'); 
     return false; 
    } else { 
     var content = '<div style="width: 200px; height: 300px;"><b>First Name in Ajax is </b>' + pa_firstname + ' <b>And Last Name in Ajax is ' + pa_lastname + '</b><button type="button" value="submit" class="LightBoxSubmit">Send Now</button><button type="button" value="back">Back</button></div>'; 
     jQuery('#preview').html(""); 
     jQuery('#preview').html(content); 
     jQuery.featherlight('#preview', {}); 
    } 
}); 

//Added function that detects click on .LightBoxSubmit which is a button in the lightbox to call a Submit event, which will trigger the function below. 
    jQuery(".LightBoxSubmit").off('click').on('click',function(e) { 
    document.getElementById("pd_test").submit(); 
    alert('LightBoxSubmit Called'); 

    }); 

//Once LightBoxSubmit button is clicked, this function is suppose to call so the Ajax (I removed the code for simplicity) event can process the $_POST data. 

jQuery('#pd_test').on('submit',function(event) { 
    event.preventDefault(); 
alert('Called Submit') 
    var pa_firstname= jQuery('input[name="pa-[name_first]"]').val(); 
    var pa_lastname= jQuery('input[name="pa-[name_last]"]').val(); 
if (pa_firstname == null || pa_firstname == '') { 
    alert('Cannot be empty'); 
} else 
    alert('Form Submitted'); 

console.log (pa_firstname); 




}); 
return false; 
}) 

問題:我無法提交按鈕,關閉燈箱,並提交表單。它不檢測on('click')事件。

+0

如果可能的話,請創建一個小提琴。 –

+0

是的,我更新了以前的小提琴,請看這個鏈接 https://jsfiddle.net/9vktzw88/3/ –

回答

0

你是相當接近,但你需要做到以下幾點:

// We first gave that dynamically generated button an id "submitformbtn". Since your button is dynamically generated, we select it as follows: 
    $(document).on('click', '#submitformbtn', function(){  
// This is the method to close current featherlight : 
     $.featherlight.current().close(); 
// Then we submit the form 
     jQuery('#pd_test').submit();  
    }); 

演示:https://jsfiddle.net/rdawkins/vvvo5ays/2/

+0

是的,它現在的作品!但是,在調用表單提交時,它沒有取得表單字段的值,pa_firstname和pa_lastname是空的。 –

+0

檢查您的代碼。你的span和你的輸入框都有相同的ID'「pa-name_first」'。這也是錯誤的:'var pa_firstname = \t jQuery('input [name =「pa- [name_first]」]').val();'只需直接選擇你的元素,其ID就像'var pa_firstname = \t jQuery(' #pa_ name_first')。val();' –

+0

非常感謝你布魯諾。 –