简体   繁体   English

将值从php发送到jquery函数

[英]send value from php to jquery function

I'm creating an employee scheduler application. 我正在创建一个员工计划程序应用程序。 I want to create a button to change the calendar view to next or previous month view. 我想创建一个按钮以将日历视图更改为下一个或上个月的视图。 But the problem is, when I click the button, the calendar changed, but the employee which has been selected to be viewed on calendar is going back to default. 但是问题是,当我单击按钮时,日历已更改,但是已选择要在日历上查看的员工将恢复为默认值。

I have this button in my php file: 我的php文件中有此按钮:

    <input type="submit" id="month-next" value="Next Month">

and I have created jquery for my button: 并且我为按钮创建了jQuery:

    $('input#month-next').click(function(id){

        if(month==12){
                ++year;
                month=0;
            }
            ++month;

        $('div#month-data').load('scheduler/ajax/calendar.php?month='+month+'&year='+year+'&empid='+id);
    });

so, how can I pass a my 'empid' value to my jquery function from the php page? 因此,如何从php页面将“ empid”值传递给我的jquery函数?

使用button而不是输入type=submit因为提交将重新加载页面。

If your jquery in same php page you can use 如果您的jQuery在同一php页面中,则可以使用

$('input#month-next').click(function(){
   var id = '<?php echo $empid ?>';

   //your remaining code
});

If your jquery at another place you need to pass as parameter in function call: 如果您的jquery在另一个地方,则需要在函数调用中作为参数传递:

<input type="button" id="month-next" value="Next Month" onclick="monthNext('<?php echo $empid; ?>');" >

Use a function instead of click event as above: 使用函数代替上述的click事件:

function monthNext(id)
{
      if(month==12)
      {
            ++year;
            month=0;
      }
      ++month;

      $('div#month-data').load('scheduler/ajax/calendar.php?month='+month+'&year='+year+'&empid='+id);
}

With HTML 5 you can use data attributes. 使用HTML 5,您可以使用数据属性。

<input type="submit" id="month-next" value="Next Month" data-empid="5">

JS: JS:

$('input#month-next').click(function(){
  id = $(this).data("empid");
  // rest of your code
}

take a hidden field where you shore the employee id and after click on the button get that hidden field value like this 在您隐藏员工ID的地方选择一个隐藏字段,然后单击按钮,获得该隐藏字段值,如下所示

$('#button').click(function(){
  emp_id = $('#emp_id').val();
  // rest of your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM