如何在自动完成功能之外显示Jquery自动完成结果

时间:2013-10-03 07:21:59

标签: jquery html jquery-ui autocomplete

此HTML代码用于选择旅行的日期和目的地。我想在自动完成功能之外显示我的结果(目标)。

如何在confirm框中调用自动完成结果(在jquery中查看if else语句)?

这是我的代码。

<!DOCTYPE html>
<html>
    <head>
        <title>Pick a Date</title>
         <link rel='stylesheet' type='text/css' href="style/jquery-ui.css"/>
        <script src="script/jquery-1.9.1.js"></script>
        <script src="script/1.10.3-jquery-ui.js"></script>
        <link rel='stylesheet' type='text/css' href='style/stylesheet.css'/>
        <script type='text/javascript' src='script/script.js'></script>
    </head>
    <body>
        <div id="header">
            <h2><br/>Select a Destination</h2>
        </div>
        <div class="left">
            <p>Departing: <input type="text" id="departing"></p>
        </div>
        <div class="right">
            <p>Returning: <input type="text" id="returning"></p>
        </div><br/>
        <div id="main">
        <div class="ui-widget">
        <label for="tags">Destination: </label>
        <input id="tags">
            <button>Submit</button>
        </div>
    </body>
</html>

Jquery的:

// JavaScript Document
$(document).ready(function() {
    $("#departing").datepicker();
    $("#returning").datepicker();
    $("button").click(function() {
        var departing = $("#departing").val();
        var returning = $("#returning").val();
        var places = ["Canada","China","Chicago","Japan","Germany","London","Moskow","Paris","Phillipins","India","Russia","Srilanka"];
  $("#tags").autocomplete({
    source: places,
    select: function(event, ui) {
                    $('#tags').text(ui.item.value);
                }
  });
        if (departing === "" || returning === "") {
            alert("Please select departing and returning dates.");
        } else {
            confirm("Would you like to go to " + ???? + " on " + departing + " and return on " + returning + "?");
        }
    });
});

1 个答案:

答案 0 :(得分:1)

怎么样

 confirm("Would you like to go to " + jQuery( '#tags' ).val() + " on " + departing + " and return on " + returning + "?");

它的作用: 在用户选择目的地之后,这将显示在。所以我们必须阅读输入值。

我们通过jQuery('#tags')访问Input-Object。现在我们通过.val()来求问。这将返回Input-field的值。

更多信息: http://api.jquery.com/val/

啊,因为这可能会引起一些刺激

jQuery('#tags')等于$('#targs')