【问题标题】:Clone Select Box Option value in same page在同一页面中克隆选择框选项值
【发布时间】:2018-05-13 22:35:31
【问题描述】:

我有一个在 cakephp 中创建的表单中的地址列,其中我们有永久地址和当前地址,其中要填写地址、国家、州、城市字段。如下图

如图所示,有一个选择框(同上),用于将所有地址字段(地址、国家、州、城市)从当前地址克隆到永久地址,供那些有相同的地址。 我们只使用印度的州所以我们在州选择框中默认设置了印度和所有印度州,所以我们只能在用户选择州后通过 ajax 调用城市选项,如代码 //选择城市的函数

 function get_city(UserProfileState)  
    {  
        var state_id=document.getElementById('UserProfileState').value;     
        $.ajax({  
            type: "GET",  
            url: "get_city",  
            data: "UserProfileState="+state_id,  
            success: function(msg){  
                $("#city").html(msg); 
            }  
        });             
    }

//克隆所有地址字段的功能

 $("#chkform").click(function()
        { 
            if($(this).is(":checked")) 
            {
                $("#UserProfilePeraddress").val($("#UserProfilePaddress").val());
                $("#UserProfilePcountry").val($("#UserProfileCountry").val());
                $("#UserProfilePstate").val($("#UserProfileState").val());
                $("#UserProfilePcity").val($("#UserProfileCity").val());
                $("#UserProfilePpincode").val($("#UserProfilePcode").val());
                $("#UserProfilePtelephone").val($("#UserProfileTelephone").val());

            }}

由于我从页面 get_city.ctp 的 ajax 调用更新了值,但是当我克隆字段时它仍然没有从城市字段中获取值,克隆 pcity 在选择框中显示空选项。

【问题讨论】:

    标签: jquery ajax cakephp clone jquery-clone


    【解决方案1】:

    尝试更改您的代码

    之前

    $("#UserProfilePcity").val($("#UserProfileCity").val());

    之后

    $("#UserProfilePcity").val($("body").find('#UserProfileCity').val());

    【讨论】:

    • 试过但结果和以前一样。 @tarikul05
    • @ankit 它应该可以工作,尝试删除缓存,另一件事是你的城市 ID #city#UserProfileCity ??
    【解决方案2】:

    我知道了,将克隆功能更新为

    if($(this).is(":checked")) 
            {
                $("#UserProfilePeraddress").val($("#UserProfilePaddress").val());
                $("#UserProfilePcountry").val($("#UserProfileCountry").val());
                $("#UserProfilePstate").val($("#UserProfileState").val());               
                var state_id=document.getElementById('UserProfileState').value;
                var city_id=document.getElementById('UserProfileCity').value;   
                $.ajax({  
                    type: "GET",  
                    url: "get_city",  
                    data: { UserProfilePState : state_id,
                        UserProfilePcity : city_id  },
                    success: function(msg){  
                        $("#pcity").html(msg); 
                    }  
                });             
                $("#UserProfilePpincode").val($("#UserProfilePcode").val());
                $("#UserProfilePtelephone").val($("#UserProfileTelephone").val());
    
            }
    

    我必须将州和城市值传递给另一个 ajax 调用。现在它可以完美运行了。

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多