简体   繁体   中英

How to preselect an option in this case?

Hi I want to know how to preselect to option. I think I can do it with javascript since I manage to get the countrycode but not the dropdown to get set appropriately:

在此处输入图片说明

In this case I want also the dropdodown to get populated to "England" which has the value GB. How can I do it? My JSP is:

<input type="text" class="inp_sel" size="2"
         name="<%=PandoraFieldConstants.FIELD_LANDKOD%>"
         id="<%=PandoraFieldConstants.FIELD_LANDKOD%>"
         value="<%if (ansokanInfo.getUppfinnareList().contains(editPerson)|| ansokanInfo.getSokandeList().contains(editPerson)){ out.write(editPerson.getLandKod());}%>"         
         onkeyup="setListbox(event, this, '<%=PandoraFieldConstants.FIELD_LAND%>')";
         onmousedown="setListboxEmpty(this)";>                                                 
         <SELECT name="<%=PandoraFieldConstants.FIELD_LAND%>" 
          id="<%=PandoraFieldConstants.FIELD_LAND%>"
          class="inp_sel" onChange="setSearchbox(this, '<%=PandoraFieldConstants.FIELD_LANDKOD%>')">
          <% for(LandKod land: pc.getLander()) { %> 
          <OPTION value="<%=land.getLandKod()%>"
          <% if (land.getLandKod().equalsIgnoreCase("SE")) {
          out.println("SELECTED");
          } %>

And my javascript is

function setListbox(e, obj, id) {

     //               sokLandkod input
     //               sokLand select

     if(e.keyCode == 13 || e.keyCode == 8) { return; }
     if (obj.value.length < 2) { return; }

     var landList = document.getElementById(id);
     var searchKod = obj.value;
     var len = searchKod.length;
     obj.value = "";

     for (var i = 0; i < landList.options.length; i++) {

                       sListValue = landList.options[i].value;
                       var matchIx = sListValue.toUpperCase().indexOf(searchKod.toUpperCase());

     if (matchIx == 0) {
                       landList.selectedIndex = i;
                       obj.value = landList.options[i].value;
                       break;
     }
     }
}


function setSearchbox(obj, id) {

     var iny = 'sokLandkod'
     var searchKod = obj.value;
     document.getElementById(id).value = searchKod;
}

          ><%=land.getLandNamn() %>
          </OPTION>
          <% } %>
          </SELECT>

Can you advice me how to set the option?

Thank you

If England's value is "GB" ... you should change the following snippet to select "GB" and not "SE" (Is that the value of Sverige?):

<% if (land.getLandKod().equalsIgnoreCase("SE")) { 
  out.println("SELECTED"); 
} %>

Unless, I misunderstood your data... It will look like this then.

<% if (land.getLandKod().equalsIgnoreCase("GB")) { 
  out.println("SELECTED"); 
} %>

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