Search code examples
javajqueryjspstruts2struts2-jquery

How to set s:hidden value from another JSP element


i have following form :

<s:form id="storno_form"
    namespace="namespace"
    action="action">

    <sj:submit id="submitId"
        key="btn.show" targets="cancelation_content"
        cssClass="button" validate="true">
    </sj:submit>
</s:form>

and outside the form is :

<sj:select
    href="%{getUrl}"
    id="selectCampaign"
    autocomplete="true"
    selectBoxIcon="true"
    key="select.campaign"
    name="campaignId"
    list="campaigns"
    listKey="id"
    listValue="name"
    headerKey="-1"
    headerValue="%{getText('select.headerValue')}"
    onSelectTopics="realod"
    onCompleteTopics="customizeAutoCompleter"
/>

how can i submit the value of the select element with the form: i have tried :

<s:form id="storno_form"
    namespace="namespace"
    action="action">
    <s:hidden  value="%{'campaignId'}" name="campaignId" />
    <sj:submit id="submitId"
        key="btn.show" targets="cancelation_content"
        cssClass="button" validate="true">
    </sj:submit>
</s:form>

but its not working ?


Solution

  • You can do this with javascript

    onchange="var el=document.getElementById('selectCampaign');document.getElementById('storno_form')['campaignId'].value=el.options[el.selectedIndex].value;"
    

    the same you can do onclick event of <sj:submit tag.

    with jQuery it's even easier

    $(document).ready(function() {
      $('#selectCampagn').change(function() {
       var val = $('#selectCampagn').val();
        $('input[name=campaignId]').val(val);
      });
    });