dopcpc9207 2014-08-04 17:40
浏览 40
已采纳

将值从一个下拉列表复制到另一个

I have a page that i have these cascading dropdowns for state, city, zip, which is used for the business address and they work good. Now i am trying to figure out a way to have a checkbox that if the user says same as business then to copy the values from business to office. Now the problem lies because the city and zip dropdowns for the office address are dynamically created based on the state choose it never copies the selection.

How can i get this to work?

Jquery code

<script>
$(document).ready(function(){
$("select#b_state").change(function(){

    var b_state =  $("select#b_state option:selected").attr('value'); 
    //alert(state); 
    $("#b_city").html( "" );
    $("#b_zip").html( "" );
    if (b_state.length > 0 ) { 
        //alert(state.length);
     $.ajax({
            type: "POST",
            url: "fetch_b_city.php",
            data: "b_state="+b_state,
            cache: false,
            beforeSend: function () { 
                $('#b_city').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#b_city").html( html );
            }
        });
    } 
});


$(document).on("change", "select#b_city",(function(){
    var b_city = $("select#b_city option:selected").attr('value');
   // alert(state_id);
    if (b_city.length > 0 ) { 
     $.ajax({
            type: "POST",
            url: "fetch_b_zip.php",
            data: "b_city="+b_city,
            cache: false,
            beforeSend: function () { 
                $('#b_zip').html('<img src="../images/loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#b_zip").html( html );
            }
        });
    } else {
        $("#b_zip").html( "" );
    }
}));


$("select#o_state").change(function(){

    var o_state =  $("select#o_state option:selected").attr('value'); 
    //alert(state); 
    $("#o_city").html( "" );
    $("#o_zip").html( "" );
    if (o_state.length > 0 ) { 
        //alert(state.length);
     $.ajax({
            type: "POST",
            url: "fetch_o_city.php",
            data: "o_state="+o_state,
            cache: false,
            beforeSend: function () { 
                $('#o_city').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#o_city").html( html );
            }
        });
    } 
});


$(document).on("change", "select#o_city",(function(){
    var o_city = $("select#o_city option:selected").attr('value');
   // alert(state_id);
    if (o_city.length > 0 ) { 
     $.ajax({
            type: "POST",
            url: "fetch_o_zip.php",
            data: "o_city="+o_city,
            cache: false,
            beforeSend: function () { 
                $('#o_zip').html('<img src="../images/loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#o_zip").html( html );
            }
        });
    } else {
        $("#o_zip").html( "" );
    }
}));



$('input[name=oi]').click(function() {
    //alert('Using the same address');  
    if ($("input[name=oi]:checked").is(':checked')) { 

        // Get value from business text boxes
        var b_address1 = $("#b_address1").val();
        var b_address2 = $("#b_address2").val();
        var b_state = $('select[name=b_state] option:selected').val();
        var b_city = $('select[name=b_city] option:selected').val();
        var b_zip = $('select[name=b_zip] option:selected').val();

        // Assign values to office text boxes
        $("#o_address1").val(b_address1);
        $("#o_address2").val(b_address2);   
        $('select[name=o_state] option[value=' + b_state + ']').attr('selected','selected');
        $('select[name=o_city] option[value=' + b_city + ']').attr('selected','selected');
        $('select[name=o_zip] option[value=' + b_zip + ']').attr('selected','selected');

      }else{
        $("#o_address1").val();
        $("#o_address2").val(); 
        $('select[name=o_state] option[value=please choose]').attr('selected','selected');
        $('select[name=o_city] option[value=please choose]').attr('selected','selected');
        $('select[name=o_zip] option[value=please choose]').attr('selected','selected');
      };
    });

});
</script>

index.php

<fieldset style="width:600px;">
                                            <legend>Bussiness Information</legend>
                                            <p></p>
                                            <p>
                                                <label for="mf">Address: </label>
                                                <input class="mf" name="b_address1" id="b_address1" type="text"/> 
                                                <!-- <span class="validate_success">A positive message!</span> -->
                                            </p>
                                            <p>
                                                <label for="mf">Address 2: </label>
                                                <input class="mf" name="b_address2" id="b_address2" type="text"/> 
                                                <!-- <span class="validate_error">A negative message!</span> -->
                                            </p>

                                            <p>
                                            <?php
                                                $sql = "SELECT DISTINCT state FROM tbl_zip ORDER BY state ASC";
                                                $query = mysqli_query($con, $sql);
                                            ?>
                                                <label>State:
                                                    <select name="b_state" id = "b_state">
                                                      <option value="">Please Select</option>
                                                      <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC )) { ?>
                                                      <option value="<?php echo $rs["state"]; ?>"><?php echo $rs["state"]; ?></option>
                                                      <?php } ?>
                                                    </select>
                                                </label>
                                            </p>
                                            <p id="b_city">

                                            </p>
                                            <p id="b_zip">

                                            </p>
                                            </fieldset>
                                            <fieldset style="width:600px;">
                                            <legend>Office Information</legend>
                                            <p>
                                            Same As Business Information: <input type="checkbox" name="oi" id="oi" onclick="sameas();" />
                                            </p>
                                            <p></p>
                                            <p>
                                                <label for="mf">Address: </label>
                                                <input class="mf" name="o_address1" id="o_address1" type="text"/> 
                                                <!-- <span class="validate_success">A positive message!</span> -->
                                            </p>
                                            <p>
                                                <label for="mf">Address 2: </label>
                                                <input class="mf" name="o_address2" id="o_address2" type="text"/> 
                                                <!-- <span class="validate_error">A negative message!</span> -->
                                            </p>

                                            <p id="o_state">
                                            <?php
                                                $sql = "SELECT DISTINCT state FROM tbl_zip ORDER BY state ASC";
                                                $query = mysqli_query($con, $sql);
                                            ?>
                                                <label>State:
                                                    <select name="o_state" id = "o_state">
                                                      <option value="">Please Select</option>
                                                      <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC )) { ?>
                                                      <option value="<?php echo $rs["state"]; ?>"><?php echo $rs["state"]; ?></option>
                                                      <?php } ?>
                                                    </select>
                                                </label>
                                            </p>
                                            <p>
                                            <label>City: 
                                                <select name="o_city" id="o_city">
                                                    <option value="">Please Select</option>
                                                 </select>
                                            </label>       
                                            </p>
                                            <p id="o_zip">

                                            </p>
                                            <p>
                                                <input class="button" type="submit" value="Submit" />
                                                <input class="button" type="reset" value="Reset" />
                                            </p>
                                        </fieldset>
  • 写回答

1条回答 默认 最新

  • dongwenghe2416 2014-08-04 17:52
    关注

    The simplest way would be just coping html from the dropdown above before setting selection:

     $("#o_zip").html($("#b_zip").html() );
     $('select[name=o_zip] option[value=' + b_zip + ']').attr('selected','selected');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线