JavaScript强制Maximo中的OnChange

时间:2012-06-05 04:27:24

标签: javascript java-ee javascript-events maximo

我目前正在为Maximo开发Bookmarklet,这是一个Java EE应用程序,我需要填充几个输入框。

通常,当用户将数据输入到框中时,他们会单击一个按钮,为其提供弹出窗口,并搜索要添加到脚本中的值。或者他们可以输入名称并点击tab / enter,然后将其转换为大写字母,并在后台执行一些操作(不确定它的确切功能)。

我目前正在使用

Javascript: $('mx1354').value = "KHBRARR"; $('mx1354').ov= "KHBRARR";

但它不像我需要的那样有效。它将输入框设置为所需的值,但它不会运行后台功能,因此当我点击保存按钮时,它不会将其识别为任何更改并丢弃我放入框中的内容。

如何模拟选项卡/输入按钮?

到目前为止,我已尝试调用onchange,focus / blur和click函数(如果我正确调用它们,则不是100%确定)。

dojo库是应用程序的一部分,所以我不确定是否可以使用它,如果它的功能或jQuery会导致冲突。

P.S。这需要在IE中运行。

OnChange功能:

function tb_(event)
{
    event = (event) ? event : ((window.event) ? window.event : "");
    if(DESIGNMODE)
        return;
    var ro = this.readOnly;
    var exc=(this.getAttribute("exc")=="1");
    switch(event.type)
    {
        case "mousedown":
            if(getFocusId()==this.id)
                this.setAttribute("stoptcclick","true");
            break;
        case "mouseup":
            if (isIE() && !hasFocus(this)) 
            {
                this.focus();
            }
            if (isBidiEnabled) 
            {
                adjustCaret(event, this); // bidi-hcg-AS
            }
            break;
        case "blur":
            input_onblur(event,this);
            if (isBidiEnabled)                                      // bidi-hcg-SC
                input_bidi_onblur(event, this);
            break;
        case "change":
            if(!ro)
                input_changed(event,this);
            break;
        case "click":
            if(overError(event,this))
                showFieldError(event,this,true);
            var liclick=this.getAttribute("liclick");
            var li=this.getAttribute("li");
            if(li!="" && liclick=="1")
            {
                frontEndEvent(getElement(li),'click');
            }

            if(this.getAttribute("stoptcclick")=="true")
            {
                event.cancelBubble=true;
            }
            this.setAttribute("stoptcclick","false");
            break;
        case "focus":
            input_onfocus(event,this);
            if (isBidiEnabled)                      // bidi-hcg-SC
                input_bidi_onfocus(event, this);
            this.select();
            break;
        case "keydown":
            this.setAttribute("keydown","true");
            if(!ro)
            {
                if(isBidiEnabled)
                    processBackspaceDelete(event,this); // bidi-hcg-AS
                if(hasKeyCode(event, 'KEYCODE_DELETE') || hasKeyCode(event, 'KEYCODE_BACKSPACE'))
                {
                    getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;                     
                }
                if((hasKeyCode(event, 'KEYCODE_TAB') || hasKeyCode(event, 'KEYCODE_ESC')))
                {
                    var taMatch = dojo.attr(this, "ta_match");
                    if(taMatch) {
                        if(taMatch.toLowerCase().indexOf(this.value.toLowerCase()) == 0) 
                        {
                            console.log("tamatch="+taMatch);
                            this.value = taMatch;
                            input_keydown(event, this);
                            dojo.attr(this, {"prekeyvalue" : ""});
                            input_forceChanged(this);
                            inputchanged = false;
                            return; // don't want to do input_keydown again so preKeyValue will work
                        }
                    }
                    if(this.getAttribute("PopupType"))
                    {
                        var popup = dijit.byId(dojohelper.getPopupId(this));
                        if (popup)
                        {
                            dojohelper.closePickerPopup(popup);
                            if(hasKeyCode(event, 'KEYCODE_ESC'))
                            {
                                if (event.preventDefault)
                                {  
                                    event.preventDefault();  
                                }
                                else
                                {  
                                    event.returnValue  = false;  
                                }  

                                return;
                            }
                        }
                    }
                }
                input_keydown(event,this);
                datespin(event,this);
            }
            else if(hasKeyCode(event,'KEYCODE_ENTER') || (hasKeyCode(event,'KEYCODE_DOWN_ARROW') && this.getAttribute("liclick")))
            {
                var lbId = this.getAttribute("li");
                frontEndEvent(getElement(lbId), 'click');
            }
            else if(hasKeyCode(event,KEYCODE_BACKSPACE))
            {
                event.cancelBubble=true;
                event.returnValue=false;
            }
            break;
        case "keypress":
            if(!ro)
            {
                if(event.ctrlKey==false && hasKeyCode(event,'KEYCODE_ENTER'))
                {
                    var db = this.getAttribute("db");
                    if(db!="")
                    {
                        sendClick(db);
                    }
                }
            }
            break;
        case "keyup":
            var keyDown = this.getAttribute("keydown");
            this.setAttribute("keydown","false");
            if(event.ctrlKey && hasKeyCode(event,'KEYCODE_SPACEBAR'))
            {
                if(showFieldError(event,this,true))
                {
                    return;
                }
                else
                {
                    menus.typeAhead(this,0);
                }
            }
            if(!ro)
            {
                if(isBidiEnabled)
                    processBidiKeys(event,this); // bidi-hcg-AS

                numericcheck(event,this);   
                var min = this.getAttribute("min");
                var max = this.getAttribute("max");

                if(min && max && min!="NONE" || max!="NONE")
                {
                    if(min!="NONE" && parseInt(this.value)<parseInt(min))
                    {
                        this.value=min;
                        getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;                     
                        this.select();
                        return false;
                    }

                    if(max!="NONE" && parseInt(this.value)>parseInt(max))
                    {
                        this.value=max;
                        getHiddenForm().elements.namedItem("changedcomponentvalue").value = this.value;
                        this.select();
                        return false;
                    }
                }
                var defaultButton = false;
                if(event.ctrlKey==false && hasKeyCode(event,'KEYCODE_ENTER'))
                {
                    var db = this.getAttribute("db");
                    if(db!="")
                    {
                        defaultButton=true;
                    }
                }
                input_changed(event,this);                  
            }
            else
            {
                setFocusId(event,this);
            }
            if(showFieldHelp(event, this))
            {
                return;
            }
            if(keyDown=="true" && hasKeyCode(event, 'KEYCODE_ENTER') && !event.ctrlKey && !event.altKey)
            {
                menus.typeAhead(this,0);
                return;
            }
            if(!hasKeyCode(event, 'KEYCODE_ENTER|KEYCODE_SHIFT|KEYCODE_CTRL|KEYCODE_ESC|KEYCODE_ALT|KEYCODE_TAB|KEYCODE_END|KEYCODE_HOME|KEYCODE_RIGHT_ARROW|KEYCODE_LEFT_ARROW')
                    && !event.ctrlKey && !event.altKey)
            {
                menus.typeAhead(this,0);
            }
            break;
        case "mousemove":
            overError(event,this);
            break;
        case "cut":
        case "paste":
            if(!ro)
            {
                var fldInfo = this.getAttribute("fldInfo");
                if(fldInfo)
                {
                    fldInfo = dojo.fromJson(fldInfo);
                    if(!fldInfo.query || fldInfo.query!=true)
                    {
                        setButtonEnabled(saveButton,true);
                    }
                }
                window.setTimeout("inputchanged=true;input_forceChanged(dojo.byId('"+this.id+"'));", 20);
            }
            break;
    }
}

4 个答案:

答案 0 :(得分:3)

一段时间后,我发现为了通过JavaScript对页面进行更改,您需要提交一个隐藏的表单,以便在后端进行验证。

以下是我用来更改输入字段值的代码。

        cc : function(e,v){
            e.focus(); //Get focus of the element
            e.value = v; //Change the value
            e.onchange(); //Call the onchange event
            e.blur(); //Unfocus the element
            console.log("TITLE === "+e.title);
            if(e.title.indexOf(v) != -1) {
                return true; //The value partially matches the requested value. No need to update
            } else {
                //Generate an hidden form and submit it to update the page with the new value
                var hiddenForm = getHiddenForm();
                var inputs = hiddenForm.elements;
                inputs.namedItem("changedcomponentid").value = e.id;
                inputs.namedItem("changedcomponentvalue").value = v;
                inputs.namedItem("event").value = "X"; //Send a Dummy Event so the script see's its invalid and sets the right Event
                submitHidden();
            }
            //Value isn't set to the required value so pass false
            return false;
        }

答案 1 :(得分:0)

运行此

input_changed(null,document.getElementById('IDHERE'));

答案 2 :(得分:0)

在maximo 7.5中,我构建了一个自定义查找

当我单击彩色超链接时,调用java脚本将值更新回父表单值或更新但保存值或未更新

  function riskmatrix_setvalue(callerId, lookupId, value,bgrColor,targetid){       
   if (document.getElementById(callerId).readOnly){
    sendEvent('selectrecord', lookupId);
    return;
}
textBoxCaller = document.getElementById(callerId);

//dojo.byId(callerId).setAttribute("value", value);
//dojo.byId(callerId).setAttribute("changed", true);
//dojohelper.input_changed_value(dojo.byId(callerId),value);
//textBoxCaller.style.background = bgrColor;
//var hiddenForm = getHiddenForm();
//if(!hiddenForm)
 //       return;
//var inputs = hiddenForm.elements;
//inputs.namedItem("event").value = "setvalue";
//inputs.namedItem("targetid").value = dojo.byId(callerId).id;
//inputs.namedItem("value").value = value;
//sendXHRFromHiddenForm();
textBoxCaller.focus(); //Get focus of the element
textBoxCaller.value = value; //Change the value
textBoxCaller.onchange(); //Call the onchange event
textBoxCaller.blur(); //Unfocus the element
//Generate an hidden form and submit it to update the page with the new value
var hiddenForm = getHiddenForm();
var inputs = hiddenForm.elements;
inputs.namedItem("changedcomponentid").value = textBoxCaller.id;
inputs.namedItem("changedcomponentvalue").value = value;
inputs.namedItem("event").value = "X"; //Send a Dummy Event so the script see's its invalid and sets the right Event
submitHidden();
sendEvent("dialogclose",lookupId);

 }

答案 3 :(得分:0)

说明

我对@ Steven10172的perfect solution进行了一些更改,并将其变成了 JavaScript可重用功能

将此作为单独的答案,因为我对我在添加上的原始答案所做的编辑被拒绝了:)

我还必须将e.onchange()行更改为e.onchange(e),因为否则文本框处理程序tb_(eventOrComponent)函数)会< strong>扔 TypeError: textbox.getAttribute is not a function


代码

var setFakeValue = function(e,v){
    console.log("Changing value for element:", e, "\nNew value:", v);

    e.focus(); //Get focus of the element
    e.value = v; //Change the value
    e.onchange(e); //Call the onchange event
    e.blur(); //Unfocus the element


    if(e.title.indexOf(v) != -1) {
        return true; //The value partially matches the requested value. No need to update
    }
    else {
        //Generate an hidden form and submit it to update the page with the new value
        var hiddenForm = getHiddenForm();
        var inputs = hiddenForm.elements;
        inputs.namedItem("changedcomponentid").value = e.id;
        inputs.namedItem("changedcomponentvalue").value = v;
        inputs.namedItem("event").value = "X"; //Send a Dummy Event so the script see's its invalid and sets the right Event
        submitHidden();
    }
    //Value isn't set to the required value so pass false
    return false;
}

用法

setFakeValue(html_element, new_value);

有趣的事实

我花了很多时间在Maximo中寻找以编程方式更改<input>值的解决方案...在某些时候,我感到非常沮丧,放弃并开始认为这是不可能的。

前段时间,我尝试完全没有期望地进行搜索,一段时间后,我找到了解决方法... Here ...

现在...正如您所看到的,这实际上只是StackOverflow的完整副本,包括问题和解决方案(用纯文本标记大声笑),但用中文...这让我感到好奇,并且经过一番搜索我在StackOverflow上找到了这篇文章。

高五到Chrome内置的网页翻译器,可让您了解该页面上的内容^^