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

Php 是否在更改和页面加载时执行jQuery函数?,php,jquery,mysql,html,Php,Jquery,Mysql,Html,这是我的情况。我有一个选择框,它是用PHP从数据库选择命令填充的。同一个select是一个select of countries,它有一个jQuery事件绑定,一旦发生更改,它就会去加载每个国家的国旗 $("#CountryA").change(function () { var countryname = this.options[this.selectedIndex]; var fileExist = $.ajax({ url: './../theme/_im

这是我的情况。我有一个选择框,它是用PHP从数据库选择命令填充的。同一个select是一个select of countries,它有一个jQuery事件绑定,一旦发生更改,它就会去加载每个国家的国旗

$("#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" />');
        }
    });
})
我们给了我一些奇怪的HTMLOptionElement,而不是所选的名称。以下是最终代码,供将来参考:D再次感谢

$(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" />');
        }
    });
}
退房:

试着做一些类似的事情:

$(function(){
    $("#CountryA").on("change", function(e) {
        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" />'); 
            }
        });
        console.log(fileExist);
    });
})

要在加载页面后运行函数,只需执行以下操作:

$(document).ready(function()
{
    //Do something here, like fetching the flag
});

我想这就是你要找的

$(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" />'); 
            }
    });
}

因此,国家选择被记住了,但该国的标志没有被记住?我已经在文档中有了该功能。准备好了吗?我尝试了您的解决方案,但似乎不起作用:我编辑了它以包含fileExist的控制台日志,查看错误是什么,还可以尝试console.log$CountryA以确保元素甚至被选中,并且existantOk,并没有错误,但我只在选择国家时得到结果,所以选择的更改也是如此。如果我按sumbit并重新加载页面,则不会执行任何操作,控制台中也不会出现任何内容,因此我猜该功能未被触发……对不起,是输入错误。应该设置为“国家国旗”图片哦,天哪,我应该看到:P这里也需要a+,让我检查一下它现在是否工作;
$(document).ready(function()
{
    //Do something here, like fetching the flag
});
$(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" />'); 
            }
    });
}