【问题标题】:Object required error Java Script对象需要错误Javascript
【发布时间】:2014-06-01 06:03:46
【问题描述】:

我对以下用于 Jira 的代码有疑问。当我运行代码时,我得到了

第 4 行的对象需要错误

请您帮忙!审批者和分配者的值是正确的,所以他们没有问题

 <script type="text/javascript" charset="utf-8" id="priorityCustomFieldScript">


//utils(common)
var by_id=function(n){
  var i=document.getElementById(n);
     return {  //error line
    value:i.value,
    set_value:function(v){i.value=v;}
  };
};


function setSummaryAndSubmit(e){

  e.preventDefault();
  e.stopPropagation();

  //utils
  var id_set=function(n){
    var v={};
    v.a=function(x,y){v[x]=y;return v;};
    v.g=function(){return v[by_id(n).value] || '';};
    return v;
  };

  //approver
  var by_id_11690=id_set('customfield_11690').
a('22468','205 SSNL SAP');

  //assignee
  var by_id_11690_1=id_set('customfield_11690:1').
a('22469','jpechea').
a('22470','amikusi');


  var setter=(function(){
    var d=new Date();
    by_id('customfield_10146').set_value(d.getFullYear());
    by_id('summary').set_value('ADI - '
+by_id('customfield_10146').value+' - '
+document.getElementById("customfield_10171").options[document.getElementById("customfield_10171").selectedIndex].text+' - '
+by_id_11690.g()+' - '
+by_id('customfield_10163').value);
    //by_id('assignee').set_value(by_id_11690_1.g());


        by_id('assignee-container').set_value(by_id_11690_1.g());
    var selectedGlobalId;
    selectedGlobalId=document.getElementById('assignee-container').value;
    jQuery("#assignee").find('option').remove();
    jQuery("#assignee").append("<option value='" + selectedGlobalId +"'>JIRA User</option>");
    jQuery("#assignee").val(selectedGlobalId).attr("selected", "selected");




  }());

  //ok
  //var form=this;
    //form.submit();
    jQuery("#customfield_10146").parents('form').submit();
}

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

function hideSummaryAndAddSeverityHook(){



        var row = document.getElementById("assignee-container");
        if (row) {
            row.style.display = 'none';
        }

        $('#customfield_10146').closest('div.field-group').hide();
        $('#summary').closest('div.field-group').hide();


        jQuery("input#issue-create-submit").click( function(event) {
            setSummaryAndSubmit(event);});
        jQuery("input#issue-edit-submit").click( function(event) {
            setSummaryAndSubmit(event);});

}

var currentLocation = window.location.href;
if (currentLocation.indexOf('CreateIssue') > -1 || currentLocation.indexOf('EditIssue') > -1)
{


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

       hideSummaryAndAddSeverityHook();

      });

   }

});
</script>

【问题讨论】:

    标签: javascript jquery error-handling


    【解决方案1】:

    document.getElementById() 如果未找到任何元素,则返回 null - 您需要在代码中进行检查。你得到的错误意味着 i 不是一个对象 - 所以 document.getElementById() 在你的一个调用中返回 null。

    【讨论】:

    • 感谢您的回答,但我如何检查哪个返回 null?
    • 检查函数本身 - var i=document.getElementById(n); if (i === null) { // uh oh? do something here }
    猜你喜欢
    • 2010-10-21
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多