开发者

Jsp with javascript parameter inside javascript onclick

开发者 https://www.devze.com 2023-04-09 10:34 出处:网络
I would like to insert a call to a function to get a parameter for my JavaScript onclickfunction. What I would like to do is something like:

I would like to insert a call to a function to get a parameter for my JavaScript onclick function. What I would like to do is something like:

<input class="refreshbutton"
       type="button"
       id="searchUfficiPopup"
       onClick="javascript:postColorbox('/DeliDete/searchUfficiPopupBySettoreId', **'&settoreIdKey=${javascript:getSettoriId()}'**, 'tabUfficiForm', 'initScriptUffici')" 
       value="<fmt:message key="navigation.searchUffici"/>" />

This way Eclipse tells me the function javascript:getSettoriId() is undefined. I defined this function in an external .js file, loaded at runtime with jQuery's .getScript, so I would not like to insert it into the jsp (anyway I tried to insert it into the jsp but the IDE still says that the function is not defined).

The function postColorbox is defined as:

function postColorbox(url, parameters, formName, initScript)

The function getSettoriId() returns the value of a previously entered form element, Settori, which I need to perform a restricted query (I need to obtain all Uffici entities related to the selected Settori entity)

Told this, I would like to ask you experts:

  1. Is it even possible to use a JavaScript function as a parameter of an onclick JavaScript function?

  2. If I put this function to be called in an external .js file will the jsp be able to see开发者_如何学JAVA it and call it?

Thank you all for your help! Andrea


Remove the onClick on your <input> and do it with a jQuery event handler instead:

$('#searchUfficiPopup').click(function() {
    var settoriId = getSettoriId();

    postColorbox('/DeliDete/searchUfficiPopupBySettoreId',
                 '&settoreIdKey=' + settoriId,
                 'tabUfficiForm',
                 'initScriptUffici');

    return false;
});


Calling functions from inside the HTML element is not a preferred way. If you can - just assign the element an id or class, and then add a listener to it using javascript on page load.

This way you don't have your data and operations mixed. It will be also easier to modify the code, as your code will be located in js files.

0

精彩评论

暂无评论...
验证码 换一张
取 消