dongliufa6380 2015-04-22 13:00
浏览 48
已采纳

JQueryUI自动完成 - 使用以前的字段值作为不同字段的条件

I am trying to have two autocompleted fields. The first one works, as it is the straightforward example of autocomplete. The second field's autocomplete validation includes the value of the first field as a condition in the query. I have been chasing this back and forward and I am unsure if it is Jquery or PHP where my issue lies.

HTML and Jquery

  <form action='' method='post'>
    <p><label>Country:</label><input type='text' id='country' name='country' value='' class='auto'></p>
    <p><label>State:</label><input type='text' id='state' name='state' value='' class='auto'></p>
  </form>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>    
<script type="text/javascript">
$(function() {
    $("#country.auto").autocomplete({
        source: "search.php",
        minLength: 1,
        change: function (event, ui){
            if (!ui.item) {
                this.value = '';
            }
        }
    }); 
});
</script>
<script>
$(function(){
    $("#state.auto").autocomplete({
        source: "statesearch.php?country=" + $("#country.auto").val(),
        minLength: 1,
        change: function (event, ui){
            if (!ui.item) {
                this.value = '';
            }
        }
    }); 


});
</script>

PHP for second field (first is working fine):

$country = $_GET['country'];

if (isset($_GET['term'])){
    $return_arr = array();

    try {
        $conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $conn->prepare('SELECT state FROM states WHERE state LIKE :term and country ='.$country);
        $stmt->execute(array('term' => '%'.$_GET['term'].'%'));

        while($row = $stmt->fetch()) {
            $return_arr[] =  $row['state'];
        }

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }


    /* Toss back results as json encoded array. */
    echo json_encode($return_arr);
}

The states table in the DB is simple, three columns, ID, country, state

Thanks in advance for any clues or steps I could go down.

  • 写回答

2条回答 默认 最新

  • doubengshao8872 2015-04-23 03:16
    关注

    You need to use the function source to do this in the current jqueryui release, per the API guide: https://api.jqueryui.com/autocomplete/#option-source

        $(function(){
        $("#state").autocomplete({
            //source: "statesearch.php?country="+country_selection,
            source: function(request, response) {
            $.ajax({
              url: "statesearch.php",
              data: {
                country : $("#country").val(),
                term : request.term
              },
              dataType: "json",
              success: function( data ) {
                response( data )
              }
            });
        },
            minLength: 1,
            change: function (event, ui){
                if (!ui.item) {
                    this.value = '';
                }
            }
        }); 
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog