dqpd4268 2012-03-22 22:24
浏览 53
已采纳

在更改和页面加载时执行jQuery函数?

here is my situation. I have a select box that is populated from a database SELECT command with PHP. The same select which is a select of countries has a jQuery event bind on it and on change it goes and load the flag of each country.

$("#CountryA").change(function () {
    var countryname = this.options[this.selectedIndex];
    var fileExist = $.ajax({
        url: './../theme/_images/flags/' + $.trim($(countryname).text()) + '.png',
        type: 'HEAD',
        error: function () {
            $("#flag").html('<img src= "./../theme/_images/flags/notFound.png" />');
        },
        success: function () {
            $("#flag").html('<img src= "./../theme/_images/flags/' + $.trim($(countryname).text()) + '.png" />');
        }
    });
})

and this is the PHP

<?php
while ($data = $connection->dbFetchArray($resultCountry)) {
    if ($data["Description"] == @$_GET['country'])
        echo "<option value='" . $data["Description"] . "' selected='selected'>" . $data["Description"] . "</option>";
    else
        echo "<option value='" . $data["Description"] . "'>" . $data["Description"] . "</option>";

}
?>

Also with php I managed to remember the selected value of the user before the sumbit. BUT after the user sumbits the form (I am using PHP_SELF so I remain in the same page) the flag dissapears.

Can I make the function to be executed again after the sumbit button? On pageload or something in order to have the flag of the selected country displayed to the user???

Thanks in advance!

======================EDIT===========================

Ok, thank you very much augustknight and everyone that took the time to respond... :D

I made some minor changes to your code because these lines

    var country_name = $("#CountryA :selected").val();
    set_country_flag_image(this.options[this.selectedIndex]);

were giving me some weird HTMLOptionElement instead of the selected name. So here is the final code for future reference. :D THanks again!!!

$(function () {
    // Initialize the flag to the selected country (could also be done server-side)
    var country_name = $("#CountryA").val();
    set_country_flag_image(country_name);


    // Update the flag image when a country is selected
    $("#CountryA").change(function () {
        alert($("#CountryA").val());
        set_country_flag_image($("#CountryA").val());
    });
});

function set_country_flag_image(country_name) {
    $.ajax({
        url: './../theme/_images/flags/' + country_name + '.png',
        type: 'HEAD',
        error: function () {
            $("#flag").html('<img src= "./../theme/_images/flags/notFound.png" />');
        },
        success: function () {
            $("#flag").html('<img src= "./../theme/_images/flags/' + country_name + '.png" />');
        }
    });
}
  • 写回答

3条回答 默认 最新

  • dpn68721 2012-03-22 22:34
    关注

    I think this is what you are looking for.

    $(function () {
        // Initialize the flag to the selected country (could also be done server-side)
        var country_name = $("#CountryA :selected").val();
        set_country_flag_image( country_name );
    
        // Update the flag image when a country is selected
        $("#CountryA").change(function(){
            set_country_flag_image(this.options[this.selectedIndex]);
        });
    });
    
    
    function set_country_flag_image(country_name)
    {
        $.ajax({
            url:'./../theme/_images/flags/'+country_name+'.png',
            type:'HEAD',
            error:
                function(){
                    $("#flag").html('<img src= "./../theme/_images/flags/notFound.png" />'); 
                },
            success:
                function(){
                   $("#flag").html('<img src= "./../theme/_images/flags/'+country_name+'.png" />'); 
                }
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效