简体   繁体   English

通过使用Ajax通过PHP从数据库中获取数据来进行预选择

[英]Pre-select option by fetching data from DB via PHP with ajax

I have a select tag, which option values come from a database via PHP using ajax. 我有一个选择标记,哪个选项值来自使用ajax通过PHP的数据库。 The previously selected value is saved in the DB. 先前选择的值保存在DB中。

I want to display a select showing the previously selected value (which is saved in the DB). 我想显示一个选择,显示先前选择的值(保存在数据库中)。

This is the HTML code 这是HTML代码

<select id='country'></select>

This is the JS code (ajax getting all country list) 这是JS代码(ajax获取所有国家/地区列表)

$.ajax({
    url: 'country.php',
    scriptCharset: 'ANSI',              
    success:function(result) {
        $('#country').append(result);
    }
});

Let's say I want to show country which value is 27, how can I achieve that? 假设我要向国家/地区显示哪个值为27,如何实现?

$.ajax({
url:"country.php",              
scriptCharset: 'ANSI' ,             
success:function(result){       
    $('#country').append(result);

    // Have you tried add this???
    $('#country').val(27);

   }
});

@thanks rahul @感谢rahul

使用选项进行另一个选择,然后在js中,您可以使用onchange重新加载国家/地区列表。

$.ajax({ $ .ajax({
url:"country.php", 网址:“ country.php”,
scriptCharset: 'ANSI' , scriptCharset:'ANSI',
success:function(result){ 成功:功能(结果){
$ ('#country').append(result); $ ('#country')。append(result);
$ ('#country').val(VALUE); $ ('#country')。val(VALUE);
} }); }});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM