为什么。post jquery函数不能在我的代码中工作?

时间:2022-09-18 12:39:39

I tried a jquery function using jQuery and it's not working

我使用jquery尝试过jquery函数,但它不起作用

It's pretty weird. I put some alerts in the js file, but when I click on the button (with id="gettotalnutrients"), none of the alerts from the js file happen.

这很奇怪。我在js文件中放置了一些警报,但是当我单击按钮(id=“gettotalnutrition”)时,js文件中的任何警报都不会发生。

this is where I grab the data for the js file (and display the data):

在这里我获取js文件的数据(并显示数据):

        <div id="feedback"></div>
    <br />
    <form id="form_date_nutrient_getter" action="account-overview.php" method="POST">
    Select a start date: <input id="datestart" type="text" name="beginday_calendar"><br />
    Select a end date: <input id="dateend" type="text" name="endday_calendar"><br />
                    <input type="button" id="gettotalnutrients" value="Get the total nutrients" ><br /><br /><br /><br /><br /><br /><br />
    </form>
    </div>

js file:

js文件:

$('#gettotalnutrients').click(function(){
           alert('test');
           var datestart = $('#datestart').val();
           alert(datestart);
           var dateend = $('#dateend').val();
           alert(dateend);
           $.post('getTotalNutrients.php',(beginday_calendar: datestart, endday_calendar: dateend), function(data){
            $('#feedback').text(data);
                });
                                });

2 个解决方案

#1


3  

wrap your click() function in $(document).ready() or something like that:

将click()函数包装为$(document).ready()或类似的内容:

$(document).ready(function() {
  $('#gettotalnutrients').click(function(){
    // the rest of your code goes here
  });
});

Also, are you seeing any errors in the js console?

另外,您是否在js控制台中看到任何错误?

#2


1  

Did you try chnaging the brackets to {

你有没有试过把括号里的东西弄到{

  $(function(){
      $('#gettotalnutrients').click(function(){
       //remaining code  

         $.post('getTotalNutrients.php',{ beginday_calendar: datestart, endday_calendar: dateend}, function(data){
                $('#feedback').text(data);
              });

           });            
    });

#1


3  

wrap your click() function in $(document).ready() or something like that:

将click()函数包装为$(document).ready()或类似的内容:

$(document).ready(function() {
  $('#gettotalnutrients').click(function(){
    // the rest of your code goes here
  });
});

Also, are you seeing any errors in the js console?

另外,您是否在js控制台中看到任何错误?

#2


1  

Did you try chnaging the brackets to {

你有没有试过把括号里的东西弄到{

  $(function(){
      $('#gettotalnutrients').click(function(){
       //remaining code  

         $.post('getTotalNutrients.php',{ beginday_calendar: datestart, endday_calendar: dateend}, function(data){
                $('#feedback').text(data);
              });

           });            
    });