在Featherlight灯箱中调用按钮

时间:2016-04-06 12:45:58

标签: javascript jquery html ajax lightbox

这是来自Featherlight hide div on Close

的后续问题

我已经成功解决了打开灯箱的问题,但我已经添加了一个提交按钮,我希望在点击时调用它('submit',function)。

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')事件。

1 个答案:

答案 0 :(得分: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/