Javascript 根据动态选择框的更改更改URL

Javascript 根据动态选择框的更改更改URL,javascript,jquery,ajax,json,html,Javascript,Jquery,Ajax,Json,Html,我有一个从JSON文件填充的选择框,我想重新加载URL,但从选择框传递值 这是我的index.html文件的精简版本 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content

我有一个从JSON文件填充的选择框,我想重新加载URL,但从选择框传递值

这是我的index.html文件的精简版本

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Timetables</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">


    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div id="navbar" class="navbar-collapse collapse">

        </div>
        <!--/.navbar-collapse -->
    </div>
</nav>

<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="jumbotron">
    <div class="container">
        <div id="headerArea">

        </div>
    </div>
</div>

</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>

    var $crs = GetURLParameter('crs');

    // Function that uses this variable removed as not relevant

    $.getJSON('csr.json', function (data) {
        var stationDropdown = '<form class="navbar-form navbar-right" action="">';
        stationDropdown += '<select class="form-control" id="stations">';
        for (var i in data) {
            stationDropdown += '<option value="' + data[i].Code + '">' + data[i].StationName + '</option>';
        }
        stationDropdown += '</select>';
        stationDropdown += '</form>';

        $("#navbar").html(stationDropdown);
    });

    $(function(){
        // bind change event to select
        $('#stations').bind('change', function () {
            var url = $(this).val(); // get selected value
            if (url) { // require a URL
                window.location = 'index.html?crs='.url; // redirect
            }
            return false;
        });
    });

</script>

</html>
选择框生成fine并被“注入”到navbar div中,但on change事件不会注册

基本上,如果有人选择Aber,onchange事件将自动重新加载index.html?crs=ABE

开发工具没有抛出任何错误,只是在我更改选择框时没有做任何事情

我怀疑我需要将on change事件作为一个函数来运行,当需要时会专门调用它,因为将它放在index.html文件的底部意味着在DOM准备就绪之前加载它

提前感谢您的帮助

您需要在上使用动态内容:

$('#navbar').on('change', '#stations', function () {
    ^ Static container content is appended to
                              ^ #stations = target element
您需要在上为动态内容使用:

$('#navbar').on('change', '#stations', function () {
    ^ Static container content is appended to
                              ^ #stations = target element

事件处理程序仅绑定到当前选定的元素;在代码进行事件绑定调用时,它们必须存在于页面上

委派事件的优点是,它们可以处理来自子元素的事件,这些子元素将在以后添加到文档中

当您动态创建元素时

你需要使用。您必须使用委托事件方法

一般语法

$(document).on(event, selector, eventHandler);
理想情况下,您应该用最接近的静态容器替换文档

范例

$("#navbar").on('change', '#stations', function(){
   //Your code
});

事件处理程序仅绑定到当前选定的元素;在代码进行事件绑定调用时,它们必须存在于页面上

委派事件的优点是,它们可以处理来自子元素的事件,这些子元素将在以后添加到文档中

当您动态创建元素时

你需要使用。您必须使用委托事件方法

一般语法

$(document).on(event, selector, eventHandler);
理想情况下,您应该用最接近的静态容器替换文档

范例

$("#navbar").on('change', '#stations', function(){
   //Your code
});

总是一样的问题。。。您可以在创建下拉列表之前启动绑定事件,因为它取决于ajax调用

您的代码有什么问题:

$.getJSON('csr.json', function (data) {  // This is done #1
        .......
        $("#navbar").html(stationDropdown); // this is done #3 : creating #stations.
    });

$('#stations').bind('change', function () { // This is done #2 and won't do anything because #stations doesn't exist yet
  ........
        });
正确的方法是:

$.getJSON('csr.json', function (data) {  // This is done #1
              .......
        $("#navbar").html(stationDropdown); // this is done #2 : creating #stations.
        $('#stations').bind('change', function () { // This is done #3
          ........})
});

总是一样的问题。。。您可以在创建下拉列表之前启动绑定事件,因为它取决于ajax调用

您的代码有什么问题:

$.getJSON('csr.json', function (data) {  // This is done #1
        .......
        $("#navbar").html(stationDropdown); // this is done #3 : creating #stations.
    });

$('#stations').bind('change', function () { // This is done #2 and won't do anything because #stations doesn't exist yet
  ........
        });
正确的方法是:

$.getJSON('csr.json', function (data) {  // This is done #1
              .......
        $("#navbar").html(stationDropdown); // this is done #2 : creating #stations.
        $('#stations').bind('change', function () { // This is done #3
          ........})
});

这是因为$getJSON函数是在$function之后调用的{
如果将$function{中的代码移到$getJSON函数的末尾,那么它就可以工作。

这是因为$getJSON函数是在$function之后调用的{
如果将$function{中的代码移到$getJSON函数的末尾,那么它就可以工作。

您应该将所有HTML构建移到HTML中

在getJSON中,您可以将代码简化为:

var stations = $( '#stations' );
for (var i in data) {
    stations.append( '<option value="' + data[i].Code + '">' + data[i].StationName + '</option>' );
}
一旦你这样做了,其他一切都应该正常工作。以下是我在你的HTML中所做的更改:

<div id="navbar" class="navbar-collapse collapse">
    <form class="navbar-form navbar-right" action="">
        <select class="form-control" id="stations">
            <option>Select One</option>
        </select>
    </form>
</div>
另外,值得注意的是,jQuery建议从1.7开始使用on方法而不是bind方法


您应该将所有HTML构建移动到HTML中

在getJSON中,您可以将代码简化为:

var stations = $( '#stations' );
for (var i in data) {
    stations.append( '<option value="' + data[i].Code + '">' + data[i].StationName + '</option>' );
}
一旦你这样做了,其他一切都应该正常工作。以下是我在你的HTML中所做的更改:

<div id="navbar" class="navbar-collapse collapse">
    <form class="navbar-form navbar-right" action="">
        <select class="form-control" id="stations">
            <option>Select One</option>
        </select>
    </form>
</div>
另外,值得注意的是,jQuery建议从1.7开始使用on方法而不是bind方法


我必须对我的更改函数做一个小更改,但这是我成功使用的答案。感谢您对我的更改函数做一个小更改,但这是我成功使用的答案。谢谢