简体   繁体   中英

Jquery, Set an option from dropdown list by a textbox value

// JQuery script
<script>
$(function(){
$('.select-change').click(function(){

???? 

//what would come here to put textbox's text of con_name_class inside dropdown1 dropdownlist!!

});
});
</script>

Now the PHP and HTML code

<?PHP

connection...blah blah

while($record=sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
$recordx = $record['x'];
$recordy = $record['y'];
$recordz = $record['z'];
}
?>


<input type="text" class="con_name_class" id="<? php somefunction_which_echos_out_a_text ?>" value="<? php 

SAMEfunction_which_echos_out_a_text ?>" />

<input class = "select-change" type="button" id="<? php echo $recordx; ?>" value = "<? php echo $recordx; ?>" /><br>

<? PHP

}


function xyz(){    // I am calling it some where!!

connection...blah blah

echo "<select name = "dropdown1" id="dropdown1">";
echo "<option value="NULL">Select one</option>";

while($rec=sqlsrv_fetch_array($stmtxyz, SQLSRV_FETCH_ASSOC)){
echo "<option value='".$rec['con_code']."')>".$rec['con_name']."</option>";
}
</select>
}
?>

How can I map the textbox's text into the dropdown1 and select it?

You may something like

  $(function() {
    $("#textboxID").on("change",function(){
         $("#selectBoxID").val($(this).val());
       //Set the option from dropdown list by textbox's value
    })
  });

Here's the plunker

Cheers!

I am modifyng NLN's code.You can give a specific part in name like "textbox_"

<input type="text" class="con_name_class" id="textbox_<? php somefunction_which_echos_out_a_text ?>" value="<? php SAMEfunction_which_echos_out_a_text ?>" />

Then use this script

 $(function() {
    $(input[id^="textbox_"]).on("change",function(){
         $("#selectBoxID").val($(this).val());
       //Set the option from dropdown list by textbox's value
    })
  });

I did it with javascript function..not jquery..because of my ID problem for the textbox. Call the below function at onClick of the textbox and pass parameters.Thanks!

Here-

    function xyz(par){
    var dd1= document.getElementbyId('dropdown1');
    var opts = dd1.options.length;

    for (var i =0; i<opts; i++){

    if(document.getElementbyId('dropdown1').options[i].value == par){
        getElementbyId('dropdown1').selectedIndex = i;
        break;
    }
  }
}

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