Calling javascript function inside jquery ajax response file code

天涯浪子 提交于 2020-01-16 08:40:09

问题


I am trying to call date_cal() javascript function inside ajax response (wall_list.php).Every thing is fine am getting correct response. But its not calling date_cal() function.

main file:

$.ajax({

  url: 'wall_list.php',
  data:"dt_from="+dt_from+"&dt_to="+dt_to+"&week="+week+"&month="+month+"&dt_filter="+dt_filter+"&fan="+fan+"&gender="+gender+"&pageNumber="+pagenumber,
  type: 'POST',
success: function (resp) { 

if(resp)
{
 //alert(resp);
  document.getElementById('wall_listdiv').innerHTML=resp;

}  

Wall_list.php

Some code...................

>   <td id="<?php print $key; ?>" class="tm_td" valign="top" colspan=2>
>   

    <script language="JavaScript">
                                date_cal('<?php print $commentcreatetimearr[$key]; ?>','<?php print $key; ?>');
                                </script>

>       </td>

Some code......................

it's not calling javascript there.

Can anyone explain how to all this function in response.


回答1:


for example
php:

<?php echo $commentcreatetimearr[$key]; ?>

js:

$.ajax({    
  url: 'wall_list.php',
  data:"dt_from="+dt_from+"&dt_to="+dt_to+"&week="+week+"&month="+month+"&dt_filter="+dt_filter+"&fan="+fan+"&gender="+gender+"&pageNumber="+pagenumber,
  type: 'POST',
success: function (resp) {     
  if(resp){
     $('#wall_listdiv').html(date_cal(resp));
  }  



回答2:


here you go

$.ajax({
    url: 'wall_list.php',
    data: "dt_from="+dt_from+"&dt_to="+dt_to+"&week="+week+"&month="+month+"&dt_filter="+dt_filter+"&fan="+fan+"&gender="+gender+"&pageNumber="+pagenumber,
    type: 'POST',
    success: function (resp){
        if(resp){
            $("#wall_listdiv").html(resp);
        }
    },
    dataType: 'html'
});

What you want to do is, specify the return dataType as html. From jQuery API

If html is specified, any embedded JavaScript inside the retrieved data is executed before the HTML is returned as a string. Similarly, script will execute the JavaScript that is pulled back from the server, then return the script itself as textual data.

More information here: jQuery.ajax() - jQuery API



来源:https://stackoverflow.com/questions/4733086/calling-javascript-function-inside-jquery-ajax-response-file-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!