Reputation: 71
I have a code that list all the values, I want written:
<option th:each = "iName : ${iNames}" th:value = "${iName}" th:text = "${iName}" th:selected="${selectedIName == iName}"
and I have setSelectedName in my .js file
function setSelectedName() {
$('#NameForTypeSearch').ready(function() {
$("#NameForSearch").change(function(){
$.ajax({
type: 'POST',
data: {keyname:$('#NameForSearch option:selected').text()}
});
});
});
I have text box named NameForTypeSearch
and everytime the user selects an option, I want to use ajax to automatically input, the selected text in NameForTypeSearch
. I can't get to work on my html.
Upvotes: 0
Views: 6917
Reputation: 305
To get the value you must use
$('#NameForSearch option:selected').val();
If you want get the option text you must use:
$('#NameForSearch option:selected').html();
if you want put this value in other input you mustn´t use Ajax, just
$('#output').val("valueToSet");
Upvotes: 0
Reputation: 3032
Try using the jquery val
function to get the value
of the option
element, as well as to set the value
of the input
element.
$('#sel').change(function(){
$('#output').val($(this).find('option:selected').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="sel">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<input type="text" id="output"/>
Upvotes: 2