This is my HTML code

这是我的HTML代码

<form class="addtowatchlistform" action="logo/insertwatchlist.php" method="POST">

    <input type="hidden" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'"/>
    <button id="addtowatchlistbutton" type="submit" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'" data-tooltip="'.$addremove.' TO YOUR WATCHLIST" class="material-icons" style="color:'.$watchlisticoncolor.'">add_box</button>

</form>

   // Same form as above
<form class="addtowatchlistform" action="logo/insertwatchlist.php" method="POST">

    <input type="hidden" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'"/>
    <button id="addtowatchlistbutton" type="submit" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'" data-tooltip="'.$addremove.' TO YOUR WATCHLIST" class="material-icons" style="color:'.$watchlisticoncolor.'">add_box</button>

</form>

Jquery Code:

Jquery代码:

<script>
$(".addtowatchlistform").submit(function(e) {
  var data = $(this).serialize();
  var url = $(this).attr("action");
  $.post(url, data, function() {
    try {
        data = JSON.parse(data);
        $("button#addtowatchlistbutton").css('color',data.watchlisticoncolor);
    } catch (e) {
        console.log("json encoding failed");
        return false;
    }
});
  return false;
});
</script>

PHP file insertwatchlist.php file

PHP文件insertwatchlist.php文件

$response = new \stdClass();
$response->watchlisticoncolor = "red";
die(json_encode($response));

Output of PHP file insertwatchlist.php file

输出PHP文件insertwatchlist.php文件

{"watchlisticoncolor":"red"}

Expected Result:

预期结果:

1.)When someone clicks on add_box button, it submits the form without reloading the page (This one works fine)

1.)当有人点击add_box按钮时,它提交表单而不重新加载页面(这个工作正常)

2.) insertwatchlist.php sends this code: {"watchlisticoncolor":"red"} and, the Jquery code place them in place of: $watchlisticoncolor variable, in the real time without reloading the page. (This one do not work),

2.)insertwatchlist.php发送此代码:{“watchlisticoncolor”:“red”},并且Jquery代码将它们代替:$ watchlisticoncolor变量,实时无需重新加载页面。 (这个不起作用),

Console tab do not show anything. Here is the screenshot of network tab, when someone clicks on the button http://prntscr.com/fxwt16

控制台选项卡不显示任何内容。以下是网络标签的屏幕截图,当有人点击按钮http://prntscr.com/fxwt16时

3 个解决方案

#1


0

Try this you have to catch data return from your ajax request in function params

试试这个你必须从函数参数中的ajax请求中捕获数据

<script>
$(".addtowatchlistform").submit(function(e) {
  var data = $(this).serialize();
  var url = $(this).attr("action");
  var form = $(this); // Add this line
  $.post(url, data, function(data) { //<---------add data here
    try {
        data = JSON.parse(data);
        $(form).children("button").css('color',data.watchlisticoncolor); //Update this line
    } catch (e) {
        console.log("json encoding failed");
        return false;
    }
});
  return false;
});
</script>

更多相关文章

  1. JavaScript 最佳实践:帮你提升代码质量
  2. jQuery Slimbox没有正确请求文件
  3. 利用javascript实现遍历xml文件的代码实例
  4. 使用jQuery导出JSON(外部文件)导入数据的度假输出
  5. JQuery纯前端导入Excel文件,兼容IE10及IE9版本以下浏览器处理方法
  6. 根据AngularJS中的条件制作输入类型文件[duplicate]
  7. 如何使用django从静态文件加载静态文件?
  8. 没有AJAX的拖放文件上传,在前台同步?
  9. 简单实现一个文件上传的进度条

随机推荐

  1. android命令行播放mp3
  2. Android补间动画原理介绍
  3. Android 软键盘功能键(EditText)
  4. android R cannot be resolved to a vari
  5. Android(安卓)模拟机键盘不可用的问题 !!
  6. Android初次使用遇到的两个有关问题
  7. Android(安卓)TextView中文字通过Spannab
  8. Android的Camera系统结构
  9. Android 启动过程详解(学习1)
  10. Android之阴影制作(Shadow)