在Javascript中传递变量不起作用

时间:2014-12-30 10:53:17

标签: javascript variables global

我试图在一个全局变量中传递一个没有运气的值来使它工作,即使我宣布它超出了函数范围,但仍然无法使其正常工作。我想要的变量是" myStart"在下面的代码中:

        var myStart; //declared as a global variable

         $(document).ready(function() {
            $("#circuit").change(function() {                

                 var getget = $.ajax({    //create an ajax request to get_dates.php
                    type: "GET",
                    url: "get_dates.php?q="+document.getElementById('circuit').value,             
                    dataType: "html",   //expect html to be returned                
                    success: function(response){                    

                        myStart = response ;
                    } //success

                });//ajax
            });//curcuit on change
         });//document.ready

然后我想输出或打印" myStart"这部分代码如下

            $('#sandbox-container .input-daterange').datepicker({
                format: "dd/mm/yyyy",
                startDate: "-"+myStart,  // it should result as "-30/06/2015" for example
                //endDate: "+15/01/2019",
                startView: 1,
                clearBtn: true,
                calendarWeeks: true,
                autoclose: true,
                todayHighlight: true
            });

我也尝试将其置于隐藏输入中,但没有任何效果。我不确定上述代码有什么问题。

提前多多感谢。

更新:完整脚本如下:

    Select One of the Circuits below:<br><br>
    <select id="circuit" name="circuit" style="font-size:20px;" on >
      <option></option>
      <option value="A" style="background-color:#FF0;">Circuit A</option>
      <option value="B" style="background-color:#6FF;">Circuit B</option>
      <option value="C" style="background-color:#FF0000;">Circuit C</option>
      <option value="D" style="background-color:#069;">Circuit D</option>
      <option value="E" style="background-color:#66CCFF;">Circuit E</option>
      <option value="F" style="background-color:#CCC;">Circuit F</option>
      <option value="G" style="background-color:#00CC00;">Circuit G</option>
      <option value="H" style="background-color:#900; color:white;">Circuit H</option>
    </select>


    <br><br>
    <div id="responsecontainer" style="font-weight:bold;"><b>Person info will be listed here.</b></div>


    <div id="sandbox-container">        
        <div class="input-daterange input-group" id="datepicker">
            Select your desired period of booking (3 months minimum) <br /><br />
            <input type="text" class="input-sm form-control" name="start" />
            <span class="input-group-addon">to</span>
            <input type="text" class="input-sm form-control" name="end" />
        </div>
    </div>



    <script type="text/javascript">
         $(document).ready(function() {
            $("#circuit").change(function() {                
                 $.ajax({    //create an ajax request to get_dates.php
                    type: "GET",
                    url: "get_dates.php?q="+document.getElementById('circuit').value,             
                    dataType: "html",   //expect html to be returned                
                    success: function(response){                    
                         $('#sandbox-container .input-daterange').datepicker({
                                format: "dd/mm/yyyy",
                                startDate: "-"+response,  // it should result as "-30/06/2015" for example
                                //endDate: "+15/01/2019",
                                startView: 1,
                                clearBtn: true,
                                calendarWeeks: true,
                                autoclose: true,
                                todayHighlight: true
                            });
                    } //success

                });//ajax

            });//curcuit on change
         });//document.ready            


    </script>

1 个答案:

答案 0 :(得分:0)

var myStart; //声明为全局变量

     $(document).ready(function() {
        $('#circuit').on('change', function() {               

             var getget = $.ajax({    //create an ajax request to get_dates.php
                type: "GET",
                url: "get_dates.php?q="+document.getElementById('circuit').value,             
                dataType: "html",   //expect html to be returned                
                success: function(response){                    

                     $('#sandbox-container .input-daterange').datepicker({
                            format: "dd/mm/yyyy",
                            startDate: "-"+response,  // it should result as "-30/06/2015" for example
                            //endDate: "+15/01/2019",
                            startView: 1,
                            clearBtn: true,
                            calendarWeeks: true,
                            autoclose: true,
                            todayHighlight: true
                        });
                } //success

            });//ajax
        });//curcuit on change
     });//document.ready

请尝试此操作..当您获得成功回复时,请直接初始化datepicker,它将解决您的问题。