php/jquery 中组合框之间的依赖关系

发布于 2024-09-14 22:23:08 字数 3002 浏览 4 评论 0原文

语言:PHP、JQuery、MySQL

问题很“简单”,我对 php 和 jquery 有三重依赖,代码工作得很好,可以插入新信息,但现在我想进行更新,所以我需要加载首先,我不知道如何添加当前信息,我添加了代码的某些部分,如果需要更多部分,请在投票之前询问我。

代码是这样的:

      <tr>
        <td class="main"><?php echo ENTRY_COUNTRY; ?></td>
        <td class="main">
                    <select id="country" name="country">
                        <option value="0">Select One...</option>
                    </select>
            </td>
      </tr>
      <tr>
        <td class="main"><?php echo ENTRY_STATE; ?></td>
        <td class="main">
                <select id="state" name="state">
                    <option value="0">Select One...</option>
                </select>
        </td>
      </tr>
          <tr>
        <td class="main"><?php echo ENTRY_CITY; ?></td>
        <td class="main">
                <select id="city" name="city">
                    <option value="0">Select One...</option>
                </select>
        </td>
      </tr>

这三个是从 mysql 中的数据库加载的,通过以下操作:

<script type="text/javascript">
$(document).ready(function(){
    cargar_paises();
    $("#country").change(function(){dependencia_estado();});
    $("#state").change(function(){dependencia_ciudad();});
    $("#city").change(function(){dependencia_zip();});
    $("#state").attr("disabled",true);
    $("#city").attr("disabled",true);
});

function cargar_paises()
{
    $.get("scripts/cargar-paises.php", function(resultado){
        if(resultado == false)
        {
            alert("Error");
        }
        else
        {
            $('#country').append(resultado);            
        }
    }); 
}
function dependencia_estado()
{
    var code = $("#country").val();
    $.get("scripts/dependencia-estado.php", { code: code },
        function(resultado)
        {
            if(resultado == false)
            {
                alert("No se encontraron provincias para ese pais");
                $("#state").attr("disabled",true);
                document.getElementById("state").options.length=1;
            }
            else
            {
                $("#state").attr("disabled",false);
                document.getElementById("state").options.length=1;
                $('#state').append(resultado);          
            }
        }

    );
}
function dependencia_ciudad()
{
    var code = $("#state").val();
    $.get("scripts/dependencia-ciudades.php?", { code: code }, function(resultado){
        if(resultado == false)
        {
            alert("Error");
        }
        else
        {
            $("#city").attr("disabled",false);
            document.getElementById("city").options.length=1;
            $('#city').append(resultado);           
        }
    });

..... 谢谢你所做的一切;)对不起英语。

Languages: PHP,JQuery,MySQL

The question is "simple", I have a triple dependency with php and jquerys, the code works great, for inserting new information, but now I would like to make an update, so I need to load the current information first and I don't figure it out how to, I add some parts of the code, if any more parts needed ask me before voting me down please.

Code is something like this:

      <tr>
        <td class="main"><?php echo ENTRY_COUNTRY; ?></td>
        <td class="main">
                    <select id="country" name="country">
                        <option value="0">Select One...</option>
                    </select>
            </td>
      </tr>
      <tr>
        <td class="main"><?php echo ENTRY_STATE; ?></td>
        <td class="main">
                <select id="state" name="state">
                    <option value="0">Select One...</option>
                </select>
        </td>
      </tr>
          <tr>
        <td class="main"><?php echo ENTRY_CITY; ?></td>
        <td class="main">
                <select id="city" name="city">
                    <option value="0">Select One...</option>
                </select>
        </td>
      </tr>

The three are loaded from a db in mysql, by actions like:

<script type="text/javascript">
$(document).ready(function(){
    cargar_paises();
    $("#country").change(function(){dependencia_estado();});
    $("#state").change(function(){dependencia_ciudad();});
    $("#city").change(function(){dependencia_zip();});
    $("#state").attr("disabled",true);
    $("#city").attr("disabled",true);
});

function cargar_paises()
{
    $.get("scripts/cargar-paises.php", function(resultado){
        if(resultado == false)
        {
            alert("Error");
        }
        else
        {
            $('#country').append(resultado);            
        }
    }); 
}
function dependencia_estado()
{
    var code = $("#country").val();
    $.get("scripts/dependencia-estado.php", { code: code },
        function(resultado)
        {
            if(resultado == false)
            {
                alert("No se encontraron provincias para ese pais");
                $("#state").attr("disabled",true);
                document.getElementById("state").options.length=1;
            }
            else
            {
                $("#state").attr("disabled",false);
                document.getElementById("state").options.length=1;
                $('#state').append(resultado);          
            }
        }

    );
}
function dependencia_ciudad()
{
    var code = $("#state").val();
    $.get("scripts/dependencia-ciudades.php?", { code: code }, function(resultado){
        if(resultado == false)
        {
            alert("Error");
        }
        else
        {
            $("#city").attr("disabled",false);
            document.getElementById("city").options.length=1;
            $('#city').append(resultado);           
        }
    });

.....
Thanks for everything ;) sorry for the english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静被遗忘 2024-09-21 22:23:08

我想你想要这样的东西:

在主页上:

<script>
document.ready = function() {
    fillBasicInformation();
}
</script>

现在你必须定义一个 fillBasicInformation 函数,如下所示:

//this should only be called in edit mode
fillBasicInformation = function() {
    //First fill country select
    //select the specific country
    //Fill state select
    //select specific state
    //fill city select
    //select specific city

    //now attach events to onChange event
    //1
}

你现在必须决定如何获取和填充字段,将选项添加到适当的位置,可能通过ajax,或者直接在页面加载并选择正确的一个(使用val() , 例如)。

I guess you want something like this:

On the main page:

<script>
document.ready = function() {
    fillBasicInformation();
}
</script>

Now you would have to define a fillBasicInformation function, as follows:

//this should only be called in edit mode
fillBasicInformation = function() {
    //First fill country select
    //select the specific country
    //Fill state select
    //select specific state
    //fill city select
    //select specific city

    //now attach events to onChange event
    //1
}

You have to decide now how you will get and fill your fields, adding options to the appropriate, probabl through ajax, or maybe directly on the page load and selecting the right one (using val(), for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文