Javascript警报不适用于特定功能

时间:2018-02-26 09:11:32

标签: javascript jquery alert

我在drupal 7中传递了一个带有按钮的表,并试图打印这些值,但我没有得到任何警报

$(document).ready(function() {
  $('#edit-title-poc').change(function() {
    alert('sdsd'); //**this alert works** 
    var poc_valueasdasd = $("#edit-title-poc option").filter(":selected").val();
    $.ajax({
      url: Drupal.settings.basePath + 'ajaxpocsdas3',
      data: {
        pocdasd: poc_valueasdasd
      },
      success: function(aasdsabc) {
        //data: value returned from server
        $('#msg-diasdsadsad').html(aasdsabc);
      }
    });
  });

  $('#poc3modulepdf-generator').click(function() {
    alert("Handler for .click() called."); //this alert is not working
  });
});

这是截屏

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

使用

$(document).on('click','#poc3modulepdf-generator',function(){
    alert('clicked')
})

您的代码无法正常工作的原因是您将事件绑定到尚未创建的元素。

使用$(document).on('click','#poc3modulepdf-generator')将事件绑定到文档。这是事件委托。有关事件委派see here

的更多详细信息

我希望这会有所帮助。