简体   繁体   English

为什么.post jquery函数不能在我的代码中运行?

[英]Why isn't the .post jquery function working in my code?

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 =“gettotalnutrients”)时,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);
                });
                                });

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控制台中看到任何错误吗?

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);
              });

           });            
    });

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

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