douqu8828 2013-03-28 09:39
浏览 35

用于获取选择框所选选项的数据的脚本

I want to fetch address of store of the city that user selects from the list of cities. I have used ajax for displaying cities.

$('.cn').change(function(){
            $('.st1 option').remove();
            $('.st1').append("<option value=''>&ltSelect city&gt;</option>");
            var curr=$(this).val();
            $.get('includes/pages/getcity.php',{cnid:curr},function(resp){
                var obj = jQuery.parseJSON(resp);
                    $.each(obj, function() { 
                        $data="<option value='"+this.id +"'>"+ this.city +"</option>";
                        $('.st1').append($data);
                    });
            });
        });
State <select name="state_id" class="select cn state_textbox" >
            <option>---Select State--</option>
            <?php 
                            $query="select * from state";
                            $result=mysql_query($query);
                            while($row=mysql_fetch_assoc($result)){
                                if($row['state_id']==0){
                                    echo "<option value='".$row['id']."'>".$row['state']."</option>";
                                }
                                }
                        ?>

        </select> <br><br><br>
        City<select class="st1 state_textbox" name="st1" style="margin-left:35px" >
                <option selected="selected">-- Select your city --</option> 
            </select>
  • 写回答

2条回答 默认 最新

  • douhuan1648 2013-03-28 09:47
    关注

    add an event to the select box to process the selected item:

    <select name="state_id" class="select cn state_textbox" onchange="get_address(this.value);" >
    

    in your jquery script:

        function get_address(state){  
            var post_url = "destination file to process the form";
                $.ajax({
                    type: "POST",
                    url: post_url,
                    datatype: "json",
                    success: function(data) 
                    {
                        $('#address').val(data.address);
                    }
                }); 
            }          
        });
    

    For sure, you are going to need to have a function running a query to read the correct address and return it back to this function! maybe something like this:(PHP Code)

    function get_address($state){
        $q = "select address from tablename where state=?";
        $query_result=$this->db->query($q,$em_code);
        $address;
        if($query_resul->result()){
            foreach ($query_resull->result() as $output) {
                $address = $output->address;
            }
        return $address;
        } else {
            return FALSE;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?