简体   繁体   中英

How do I trigger a struts action from within javascript?

I have searched the web for this but so far got nowhere. I have the following javascript code:

function trigger(){
                var fdate = document.getElementById("d_date").value;
                var m = fdate.substring(fdate.indexOf('/')+1, fdate.lastIndexOf('/'));
                var y = fdate.slice(fdate.lastIndexOf('/')+1);

                form.action="/toEditDate.do?month="+m+"&year="+y+"&navigate";
                form.submit();

            }

an the following jsp element:

<html:text property="strCreationDate" size="10" maxlength="10" title="Date" 
styleId="d_date" onclick="displayCalendar(document.forms[0].strCreationDate,'dd/mm/yyyy',this)"
onchange="trigger();"/>

The code is adapted from what I've managed to dig up searching online. Only problem is when I change the text in the textbox nothing happens.

I seems you need to pass in the form instance

function trigger(form){
            var fdate = document.getElementById("d_date").value;
            var m = fdate.substring(fdate.indexOf('/')+1, fdate.lastIndexOf('/'));
            var y = fdate.slice(fdate.lastIndexOf('/')+1);

            form.action="/toEditDate.do?month="+m+"&year="+y+"&navigate";
            form.submit();

        }

And the jsp change

<html:text property="strCreationDate" size="10" maxlength="10" title="Date" 
styleId="d_date" onclick="displayCalendar(document.forms[0].strCreationDate,'dd/mm/yyyy',this)"
onchange="trigger(document.getElementById('formId'));"/>

or if only one form on the page

<html:text property="strCreationDate" size="10" maxlength="10" title="Date" 
styleId="d_date" onclick="displayCalendar(document.forms[0].strCreationDate,'dd/mm/yyyy',this)"
onchange="trigger(document.forms[0]);"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM