JQuery在对话框加载时运行Javascript

4

我为JIRA实例编写了一些Javascript代码,用于在创建问题时自动填充字段。如果我在新窗口中打开“创建问题”屏幕,它的效果就像预期的那样。 然而,当“创建问题”出现在对话框中时,我的脚本不会运行。 如何使我的脚本能够在“创建问题”对话框屏幕在窗口中打开时运行? 以下是我的代码:

/*
 * Automatically fills the summary field of an issue and hides the field at issue creation.
*/

jQuery(document).ready(function($) {

JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
    autoFillSummary();
});

autoFillSummary();

// Automatically fill the summary field with a default value.
function autoFillSummary(){
    var issueType = $('#issue-create-issue-type').text();
    var reporter = $('#reporter').val();
    var d = new Date();
    d = d.toDateString();
    var summaryVal = reporter + " - " + "[ " + d + " ]";
    if(issueType === "Job"){
        $('#summary').val(summaryVal);
        $('#summary').closest('div.field-group').hide();
        $('#reporter').closest('div.field-group').hide();
    }
}  

});

创建问题的对话框ID为create-issue-dialog。非常感谢您提供的任何帮助。
3个回答

3
我会这样做:

我会采用以下方法:

jQuery(document).ready(function($) {

  $('#create-issue-dialog').dialog({
    autoOpen: false,
    open: function(event, ui) {
      JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function(e, context) {
        autoFillSummary();
      });
    }
  });

  // Automatically fill the summary field with a default value.
  function autoFillSummary() {
    var issueType = $('#issue-create-issue-type').text();
    var reporter = $('#reporter').val();
    var d = new Date();
    d = d.toDateString();
    var summaryVal = reporter + " - " + "[ " + d + " ]";
    if (issueType === "Job") {
      $('#summary').val(summaryVal);
      $('#summary').closest('div.field-group').hide();
      $('#reporter').closest('div.field-group').hide();
    }
  }
});

我从未使用过JIRA。


这似乎不是解决方案,但感谢您的帮助和快速响应。 - MasterDex

1
AJS.toInit(function () {
    autoFillSummary();
});

JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
    autoFillSummary();
});

// Automatically fill the summary field with a default value.
function autoFillSummary(){
    var issueType = $('#issue-create-issue-type').text();
    var reporter = $('#reporter').val();
    var d = new Date();
    d = d.toDateString();
    var summaryVal = reporter + " - " + "[ " + d + " ]";
    if(issueType === "Job"){
        $('#summary').val(summaryVal);
        $('#summary').closest('div.field-group').hide();
        $('#reporter').closest('div.field-group').hide();
    }
}  

尝试添加AJS.toInit(function () { ... });,它可以在创建问题表单中运行您的脚本。

0

我还没有找到理想的解决方案,但是作为一种解决方法,我已经成功地禁用了对话框的出现,并强制屏幕加载到一个新页面:

/*
 * Automatically fills the summary field of an issue and hides the field at issue creation.
 */

jQuery(document).ready(function($) {

// Disable pop-up window - Forces 'Create Issue' to load in new page.
AJS.$("#create_link").removeClass("create-issue");

autoFillSummary();

// Automatically fill the summary field with a default value.
function autoFillSummary(){
    var issueType = AJS.$('#issue-create-issue-type').text();
    var reporter = AJS.$('#reporter').val();
    var d = new Date();
    d = d.toDateString();
    var summaryVal = reporter + " - " + "[ " + d + " ]";
    if(issueType === "Job"){
        AJS.$('#summary').val(summaryVal);
        AJS.$('#summary').closest('div.field-group').hide();
        AJS.$('#reporter').closest('div.field-group').hide();
    }
}  
});

如果有人找到了避免禁用对话框的方法,将不胜感激。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接