3

How should I get an form input selected value using jQuery

<form:select path="amtAppMonitResltVO.monitStat" id="amtAppMonitResltVO.monitStat" cssClass="state">
  <option value="">선택</option>
  <form:options name="monit" items="${apps}" itemValue="subCd" itemLabel="subCdNm" />
</form:select> 
<img src="<c:url value='/resources/img/read.png'/>" class="read" id="appStatSearch"></td>
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
min moica
  • 139
  • 2
  • 12

2 Answers2

4

You just need get the .val() of the amtAppCollInfoVO_mkType select input:

var some_var = $('#amtAppCollInfoVO_mkType').val();

For pure Javascript just switch .val() to .value

Note that your current option does not have a value.

Note that since you have the . in your id you have to add double \ to your Javascript code:

<script>
    $(function() {
        $('#amtAppMonitResltVO\\.monitStat').on('click', function() {
            var moniStat = $('#amtAppMonitResltVO\\.monitStat').val();

            console.log(moniStat);
        })
    })
</script>

Credit goes here for this answer.

Sam
  • 2,856
  • 3
  • 18
  • 29
  • Right, have you attempted to add a `value` to your option? The parameter is empty. You could also try something like `$('select[id= amtAppCollInfoVO_mkType]').val()` if it is still returns null – Sam Sep 21 '17 at 01:50
  • What exactly are you seeing? Can you give the console error report? – Sam Sep 21 '17 at 01:54
  • I tried $("#amtAppMonitResltVO.monitStat").val() and It gives undefined – min moica Sep 21 '17 at 01:57
  • Are you just doing that on load? First, remove that period between the fields in the `ID`. Then Try to add a function and wait for a `change` on the value of your select: `` – Sam Sep 21 '17 at 02:06
  • Samuel U missed .monitStat ? path="amtAppMonitResltVO.monitStat" I tried it in detail.js console.log('selected value: ',$("#amtAppMonitResltVO.monitStat").val()); it gives undefined – min moica Sep 21 '17 at 02:27
  • is itemValue just same as value? – min moica Sep 21 '17 at 02:29
  • I meant to leave `.monitStat` out, it is the reason you are getting the error man. @minmoica – Sam Sep 21 '17 at 02:30
  • it gives undefined without .monitStat and also there are many ohter form with amtAppMonitResultVO like amtAppMonitResltVO.monitStat,amtAppMonitResltVO.paymMethd in same jsp page – min moica Sep 21 '17 at 02:34
  • @minmoica use (this answer right here)[https://stackoverflow.com/a/605835/1527252]. This should fix your issue by adding double `\\` before the period – Sam Sep 21 '17 at 02:36
  • @minmoica I've updated my answer for you to see what I am talking about. – Sam Sep 21 '17 at 02:42
  • Samual I gave the seperate Id like amtAppMonitResultVO_monitStat and it work – min moica Sep 21 '17 at 03:53
0

The one option you have has no value, but if you set a value for it, you could use a selector like the following to get the value of the first selected option.
$("#amtAppCollInfoVO_mkType option:selected").val()
If you wanted all the option values in an array you could use the map method. var arr = $("#amtAppCollInfoVO_mkType option").map(function(){return $(this).val();}).get();