Custom function with Ajax not working as expected

I am bashing my head with this problem whole day. I lack good knowledge of JavaScript, and for DOM manipulation i started to use jQuery. It is easy to construct some basic things. What i wanted is to build custom function that will populate form with list that will be pulled out of database based on input parameters that i pass to this function. The code would look like this: (function($) { $.fn.multiSelect = function(a,b,c) { var someVar1 = parseInt(a,10); var someVar2 = parseInt(b,10); var someVar3 = c; var dataString = "a=" + someVar1 + "&b=" + someVar2 + "&c=" + someVar3; $.ajax({ type: "POST", url: "test_ajax.php", data: dataString, dataType: 'json', success: function(data) { var a1 = data['a']; var b1 = data['b']; var c1 = data['c']; return $(this).html('

' + a1 + b1 + c1 + '

'); } }); return $(this).html('

' + someVar1 + someVar2 + someVar3 + '

'); } }; }) (jQuery); And PHP code inside test_ajax.php, is: $_POST['a'], 'b'=> $_POST['b'], 'c'=> $_POST['b'] ); echo json_encode($_something); } ?> Now when i use my jQuery function like this: $('#someDiv').multiSelect(1,1,'user'); Function in side $.(ajax), returns nothing, and outside of it (that i created just to see what is passed in to the function) returns "11user". And if I use above code like this: $(function() { var dataString = 'a=' + 1 + '&b=' + 1 + '&c=user'; $.ajax({ type: "POST", url: "test_ajax.php", data: dataString, dataType: 'json', success: function(data) { var a1 = data['a']; var b1 = data['b']; var c1 = data['c']; $('#someDiv').html('

' + a1 + b1 + c1 + '

'); } }); }); It fetches data from test_ajax.php script. All of this is simple just to troubleshoot problem, then i am going to build complex PHP script that will do it's job with database. I am guessing that i am doing something wrong with variables (local/global) or incorrect jQuery function chaining. EDIT: Since i would repeat this operation many times on different menus, function makes more logic then repeating whole code over and over. Any help would be appreciated.

以上就是Custom function with Ajax not working as expected的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏