简体   繁体   中英

Ajax response from textbox to select option

Below is my code, everything is working. I am giving input to first textbox and getting ajax response to second textbox and than select option show the same value as second textbox but select option only change when i press arrow key after putting cursor in second textbox, here i want to change select option without putting cursor or pressing any key.

Code:

<script>
$(document).ready(function() {
    $('#searchip').change(function(){
        $.ajax({

            type: "GET",
            url: "reverseip.php",
            data: 'result=' + $('#searchip').val(),

            success: function(output){
                $('#resultip').val(output);

            }

        }); // Ajax Call



    }); //event handler


 }); //document.ready
</script>


<script type="text/javascript">
$(function() {
$('#resultip').on('keyup', function() {
    selectByText($.trim($(this).val()));
});
});

function selectByText(txt) {
$('#MySelect option')
    .filter(function() { 
        return $.trim($(this).text()) == txt; 
    })
    .attr('selected', true);
}
</script>


<input type="text" id="searchip">
<input type="text" id="resultip">




<select name="sel" id="MySelect" >
<option value="">Select One</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>



How to change selectbox option without pressing arrow key in second textbox?

Call the same function when you set the value of the text box:

success: function(output){
  $('#resultip').val(output);
  selectByText($.trim($('#resultip').val()));
}

Make change in ajax call success function as follows:

success: function(output){
    $('#resultip').val(output);
    $('#MySelect option[value='+output+']').attr('selected','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