Forms 使用jQuery和AJAX传递变量自动刷新Div内容

Forms 使用jQuery和AJAX传递变量自动刷新Div内容,forms,variables,jquery,Forms,Variables,Jquery,我试图设置一个页面,用户可以从选择菜单中选择1个条件,然后从另一个菜单中选择第二个条件 然后,我希望这些变量通过使用ajax的自动更新div,以便在刷新的.php中使用它们 选择菜单工作正常,但是我如何通过ajax传递值,然后确保它在刷新时记住这些值呢 形式 并用于数据库查询 编辑 **低级$.ajax方法包含一个名为data的变量,您可以通过该变量将数据传递给服务器。因此,如果您希望在这种情况下发送数据,可以执行以下操作: $.ajax({ data: {year:1999,usernam

我试图设置一个页面,用户可以从选择菜单中选择1个条件,然后从另一个菜单中选择第二个条件

然后,我希望这些变量通过使用ajax的自动更新div,以便在刷新的.php中使用它们

选择菜单工作正常,但是我如何通过ajax传递值,然后确保它在刷新时记住这些值呢

形式

并用于数据库查询

编辑

**低级$.ajax方法包含一个名为data的变量,您可以通过该变量将数据传递给服务器。因此,如果您希望在这种情况下发送数据,可以执行以下操作:

$.ajax({
  data: {year:1999,username:'Codded'}
});
load也是一样,第二个参数用于传递数据,所以您可以这样做

$("#divId").load('foo.php',{username:'Codded',year:1999});
低级$.ajax方法包含一个名为data的变量,您可以通过该变量将数据传递给服务器。因此,如果您希望在这种情况下发送数据,可以执行以下操作:

$.ajax({
  data: {year:1999,username:'Codded'}
});
load也是一样,第二个参数用于传递数据,所以您可以这样做

$("#divId").load('foo.php',{username:'Codded',year:1999});

u可以使用ajax函数的数据属性传递所选值,如以下示例所示:

$.ajax({
data: {'name': 'Dan'} // name is the name of the variable and Dan is the value in your case if the selected value 
});

u可以使用ajax函数的数据属性传递所选值,如以下示例所示:

$.ajax({
data: {'name': 'Dan'} // name is the name of the variable and Dan is the value in your case if the selected value 
});

检查您的select id是否与下面给出的相同,因为它与您编码的select框不匹配

var user_select= $j('#user_select').val();
var year_select= $j('#year_select').val();

检查您的select id是否与下面给出的相同,因为它与您编码的select框不匹配

var user_select= $j('#user_select').val();
var year_select= $j('#year_select').val();

下面的代码成功地将值传递到php页面

<script type="text/javascript">
    $(document).ready(function(){
        $("#they").change(function () {
        var firstVal = $("#employee_user").val();
        var secVal = $("#they").val();

        $.ajax({
                type: "POST",
                data: "year="+ firstVal +"&username="+ secVal,
                url: "getholidaylog.php",
                success: function(msg){
                $("#updatingdiv").html(msg);

                }
            });
        });
    }); 
</script>

但是不要将getholidaylog.php加载到div中,而是将相应页面的内容加载到div中,下面的代码成功地将值传递到php页面

<script type="text/javascript">
    $(document).ready(function(){
        $("#they").change(function () {
        var firstVal = $("#employee_user").val();
        var secVal = $("#they").val();

        $.ajax({
                type: "POST",
                data: "year="+ firstVal +"&username="+ secVal,
                url: "getholidaylog.php",
                success: function(msg){
                $("#updatingdiv").html(msg);

                }
            });
        });
    }); 
</script>

但是不要将getholidaylog.php加载到div中,而是将相应页面的内容加载到div中

在html中添加两个隐藏字段,如下所示:

-------
<input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form>
<select id="employee_user"> 
<option value="">--</option> 
<option value="333">Test User</option> 
<option value="111">Testing Testing</option>     
</select> 

<select id="they" onchange="showUser(this.value, employee_user.value)"> 
<option value="">--</option> 
<option value="20120801" class="333" title="Test User">20120801</option> 
<option value="20110801" class="333" title="Test User">20110801</option> 
<option value="20100801" class="333" title="Test User">20100801</option> 
<option value="20120801" class="111" title="Testing Testing">20120801</option> 
<option value="20110801" class="111" title="Testing Testing">20110801</option> 
</select> 
<select id="employee_user"> 
<option value="">--</option> 
<option value="333">Test User</option> 
<option value="111">Testing Testing</option>     
</select> 

<select id="they" onchange="showUser(this.value, employee_user.value)"> 
<option value="">--</option> 
<option value="20120801" class="333" title="Test User">20120801</option> 
<option value="20110801" class="333" title="Test User">20110801</option> 
<option value="20100801" class="333" title="Test User">20100801</option> 
<option value="20120801" class="111" title="Testing Testing">20120801</option> 
<option value="20110801" class="111" title="Testing Testing">20110801</option> 
</select> 
 <input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form> 
$(function(){
      $("#year_select").change(function (){                   
                    $('#userselect').val($('#employee_user').val());
                    $('#yearselect').val($('#they').val());
                });

    $.ajaxSetup(
    {
        cache: false,
        beforeSend: function() {
            $('#updatingdiv').hide();
            $('#loading').show();
        var user_select= $j('#userselect').val();
        var year_select= $j('#yearselect').val();
        },
        complete: function() {
            $('#loading').hide();
            $('#updatingdiv').show();
        },
        success: function() {
            $('#loading').hide();
            $('#updatingdiv').show();
        }
    });

    $container.load('getholidaylog.php',{username:user_select,year:year_select}); 
    var refreshId = setInterval(function()
    {
        $container.load('getholidaylog.php');
    }, 9000);
})
现在从隐藏字段中获取值。更改这些行

        var user_select= $j('#userselect').val();
        var year_select= $j('#yearselect').val();
因此,最终的hmtl标记如下所示:

-------
<input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form>
<select id="employee_user"> 
<option value="">--</option> 
<option value="333">Test User</option> 
<option value="111">Testing Testing</option>     
</select> 

<select id="they" onchange="showUser(this.value, employee_user.value)"> 
<option value="">--</option> 
<option value="20120801" class="333" title="Test User">20120801</option> 
<option value="20110801" class="333" title="Test User">20110801</option> 
<option value="20100801" class="333" title="Test User">20100801</option> 
<option value="20120801" class="111" title="Testing Testing">20120801</option> 
<option value="20110801" class="111" title="Testing Testing">20110801</option> 
</select> 
<select id="employee_user"> 
<option value="">--</option> 
<option value="333">Test User</option> 
<option value="111">Testing Testing</option>     
</select> 

<select id="they" onchange="showUser(this.value, employee_user.value)"> 
<option value="">--</option> 
<option value="20120801" class="333" title="Test User">20120801</option> 
<option value="20110801" class="333" title="Test User">20110801</option> 
<option value="20100801" class="333" title="Test User">20100801</option> 
<option value="20120801" class="111" title="Testing Testing">20120801</option> 
<option value="20110801" class="111" title="Testing Testing">20110801</option> 
</select> 
 <input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form> 
$(function(){
      $("#year_select").change(function (){                   
                    $('#userselect').val($('#employee_user').val());
                    $('#yearselect').val($('#they').val());
                });

    $.ajaxSetup(
    {
        cache: false,
        beforeSend: function() {
            $('#updatingdiv').hide();
            $('#loading').show();
        var user_select= $j('#userselect').val();
        var year_select= $j('#yearselect').val();
        },
        complete: function() {
            $('#loading').hide();
            $('#updatingdiv').show();
        },
        success: function() {
            $('#loading').hide();
            $('#updatingdiv').show();
        }
    });

    $container.load('getholidaylog.php',{username:user_select,year:year_select}); 
    var refreshId = setInterval(function()
    {
        $container.load('getholidaylog.php');
    }, 9000);
})

做这样的事

在html中添加两个隐藏字段,如下所示:

-------
<input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form>
<select id="employee_user"> 
<option value="">--</option> 
<option value="333">Test User</option> 
<option value="111">Testing Testing</option>     
</select> 

<select id="they" onchange="showUser(this.value, employee_user.value)"> 
<option value="">--</option> 
<option value="20120801" class="333" title="Test User">20120801</option> 
<option value="20110801" class="333" title="Test User">20110801</option> 
<option value="20100801" class="333" title="Test User">20100801</option> 
<option value="20120801" class="111" title="Testing Testing">20120801</option> 
<option value="20110801" class="111" title="Testing Testing">20110801</option> 
</select> 
<select id="employee_user"> 
<option value="">--</option> 
<option value="333">Test User</option> 
<option value="111">Testing Testing</option>     
</select> 

<select id="they" onchange="showUser(this.value, employee_user.value)"> 
<option value="">--</option> 
<option value="20120801" class="333" title="Test User">20120801</option> 
<option value="20110801" class="333" title="Test User">20110801</option> 
<option value="20100801" class="333" title="Test User">20100801</option> 
<option value="20120801" class="111" title="Testing Testing">20120801</option> 
<option value="20110801" class="111" title="Testing Testing">20110801</option> 
</select> 
 <input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form> 
$(function(){
      $("#year_select").change(function (){                   
                    $('#userselect').val($('#employee_user').val());
                    $('#yearselect').val($('#they').val());
                });

    $.ajaxSetup(
    {
        cache: false,
        beforeSend: function() {
            $('#updatingdiv').hide();
            $('#loading').show();
        var user_select= $j('#userselect').val();
        var year_select= $j('#yearselect').val();
        },
        complete: function() {
            $('#loading').hide();
            $('#updatingdiv').show();
        },
        success: function() {
            $('#loading').hide();
            $('#updatingdiv').show();
        }
    });

    $container.load('getholidaylog.php',{username:user_select,year:year_select}); 
    var refreshId = setInterval(function()
    {
        $container.load('getholidaylog.php');
    }, 9000);
})
现在从隐藏字段中获取值。更改这些行

        var user_select= $j('#userselect').val();
        var year_select= $j('#yearselect').val();
因此,最终的hmtl标记如下所示:

-------
<input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form>
<select id="employee_user"> 
<option value="">--</option> 
<option value="333">Test User</option> 
<option value="111">Testing Testing</option>     
</select> 

<select id="they" onchange="showUser(this.value, employee_user.value)"> 
<option value="">--</option> 
<option value="20120801" class="333" title="Test User">20120801</option> 
<option value="20110801" class="333" title="Test User">20110801</option> 
<option value="20100801" class="333" title="Test User">20100801</option> 
<option value="20120801" class="111" title="Testing Testing">20120801</option> 
<option value="20110801" class="111" title="Testing Testing">20110801</option> 
</select> 
<select id="employee_user"> 
<option value="">--</option> 
<option value="333">Test User</option> 
<option value="111">Testing Testing</option>     
</select> 

<select id="they" onchange="showUser(this.value, employee_user.value)"> 
<option value="">--</option> 
<option value="20120801" class="333" title="Test User">20120801</option> 
<option value="20110801" class="333" title="Test User">20110801</option> 
<option value="20100801" class="333" title="Test User">20100801</option> 
<option value="20120801" class="111" title="Testing Testing">20120801</option> 
<option value="20110801" class="111" title="Testing Testing">20110801</option> 
</select> 
 <input type="hidden" id="userselect" />
<input type="hidden" id="yearselect" />
</form> 
$(function(){
      $("#year_select").change(function (){                   
                    $('#userselect').val($('#employee_user').val());
                    $('#yearselect').val($('#they').val());
                });

    $.ajaxSetup(
    {
        cache: false,
        beforeSend: function() {
            $('#updatingdiv').hide();
            $('#loading').show();
        var user_select= $j('#userselect').val();
        var year_select= $j('#yearselect').val();
        },
        complete: function() {
            $('#loading').hide();
            $('#updatingdiv').show();
        },
        success: function() {
            $('#loading').hide();
            $('#updatingdiv').show();
        }
    });

    $container.load('getholidaylog.php',{username:user_select,year:year_select}); 
    var refreshId = setInterval(function()
    {
        $container.load('getholidaylog.php');
    }, 9000);
})

我已经编辑了我的问题,我将如何将您的方法包括在其中我已经编辑了我的问题,我将如何将您的方法包括在其中您忘记在setInterval方法内的Ajax调用中发送数据。oops,ok添加了该方法,但仍然不起作用。当我更改第二个selectYou忘记在setInterval方法内的Ajax调用中发送数据时,什么也没有发生。oops,ok添加了这个,但仍然不起作用。当我更改第二个选择时,不会发生任何事情