javascript - Create a dynamic JQuery to disable a dropdown -
i working servlet , jsp. have jsp form in have drop down list getting generated dynamically of have 3 options in drop down list.
html code--
<form id='form' method='post' action='/test/'> <select id="type" name="typeone"> <optgroup id="first" name="first" label="firstdiv"> <option value="value1" id="001">valuee1 count</option> <option value="value2" id="002">value2</option> <option value="value3" id="003">value3 </option> </optgroup> </select> </form>
now need disable few of options in above drop down list using jquery. below jquery in disabling 001
, 002
options have hardcoded id's in below jquery -
<script> $(document).ready(function () { $(function () { $("#type option[id='001']").prop("disabled", true); $("#type option[id='002']").prop("disabled", true); }); }); </script>
below servlet passing list have id's want disable in drop down list.
list<string> storetest = new arraylist<string>(); storetest.add("001"); storetest.add("002"); req.setattribute("store", storetest);
problem statement:-
now question - how iterate store
list in jquery , disable 001
, 002
options in drop down list?
prepare selectors in servlet
, inject in jsp
:
myservlet.java
list<string> storetest = new arraylist<string>(); storetest.add("001"); storetest.add("002"); // if have java8 : use lamda list.stream().map(e->'#'+e) req.setattribute("store",stringutils.join(mapforid(storetest).toarray(),",")); this.getservletcontext().getrequestdispatcher( "/mypage.jsp" ).forward( req, response );
then in jsp , adding expression following:
mypage.jsp
<script type="text/javascript"> //.... jquery(document).ready(function () { jquery(function () { jquery(<%=request.getattribute("store") %>).prop("disabled", true); }); }); </script>
known mapforid method in servlet :
list<string> mapforid(list l){ list<string> newarr=new arraylist<string>(); for(string attr : l){ newarr.add("option#"+attr); } return newarr; }
Comments
Post a Comment