Ajax部分无法正常工作

时间:2014-10-01 21:29:57

标签: javascript ajax jquery-mobile

我正试图让ajax部分工作,但我没有运气。我尝试将其放在不同的部分,但变量“选择”不会通过,页面不会更新。基本上我要做的是获取在下拉输入项中输入的值,然后使用该值在Ajax函数中创建表。

<html>
<link rel="stylesheet" href="http://code.jquery.com/mobile/git/jquery.mobile-git.css"> 
<script src="http://code.jquery.com/jquery-1.10.2.js"></script> 
<script src="http://code.jquery.com/mobile/git/jquery.mobile-git.js"></script> 

<script>

    $( document ).on( "pagecreate", function() {
        $( "#mylist li" ).on( "click", function() {
            $( "#myinput" ).val( $( this ).text() );  
            $("#mylist li" ).addClass('ui-screen-hidden');

            var selection = $( "#myinput" ).val(); 
            var station_code = "null";

            if (selection == "Location1")
            {
                station_code = "A254"; 
                my_url="www.someurl.com/"+station_code;
            }
            else{
                station_code = "A300";
            }

            $.ajax({
                 type:"get",    
                 dataType: 'jsonp',
                 url: "www.someurl.com",
                success: function(data) {
                    //write code
                }
            });

        });
    });

</script> 

<div data-role="content" id="content">
<form class="ui-filterable">
    <input type="text" id="myinput">
</form>
<ul data-role="listview" data-inset="true" data-filter="true" data-filter-reveal="true" data-input="#myinput" id="mylist">
    <li>location1</li>
    <li>location2</li>
    <li>location3</li>
</ul>
</div>

1 个答案:

答案 0 :(得分:1)

我认为你的javascript代码有点乱。试试这个:

   $( document ).on( "pagecreate", function() {
        $( "#mylist li" ).on( "click", function() {
            $( "#myinput" ).val( $( this ).text() );  
            $("#mylist li" ).addClass('ui-screen-hidden');

            var selection = $( "#myinput" ).val(); 
            var location_code = "null";
            var my_url = "null";

            if (selection == "Elm St"){
                 location_code = "R227";
             }

            my_url="www.somelink.com";            

            $.ajax({        
                type: "post",
                url: my_url,
                dataType: 'jsonp',
                success: function(data){        
                    document.write(data);
                },
                error: function(){
                    document.write ("didn't work");
                }
            });
        });
    });