weixin_33725126 2015-08-31 15:46 采纳率: 0%
浏览 39

调用jQuery ajax函数

Please bear with me; I've been battling with this for days.

I have a little bit of jQuery code in my jQuery(document).ready function as follows:

        jQuery(document).ready(function($) {
        jQuery.post(ajaxurl, data, function(response) {
          alert(response);
        });
    });

I have a text input field which I want sent to my Ajax function (using the Wordpress API)

    <input type="text" name="input" id=input onkeydown="if (event.keyCode == 13) {send_message(this.value);}">

If the user presses the key, the text is sent to the javascript 'send_message' function (below).

function send_message(value){
  data[txt]=value;
  jQuery.post(ajaxurl, data, function(response) {
    alert(response);
  });
}

BAsically, I've added a 'txt' key/value pair to my data object and I now want this submitted to my ajax function.

The 'document ready' part works fine, but I can't get it to accept the changed data object.

As you can probably tell, I'm not following the logic behind the jQuery code very well.

  • 写回答

2条回答 默认 最新

  • weixin_33675507 2015-08-31 15:56
    关注

    The data parameter in your function is not initialized. You should probably see a javascript error if you inspect your page using chrome or firefox.

    Basically the data parameter in jquery's post function accepts either a serialized form query string or JSON string/object.

    Also, please check the api: http://api.jquery.com/jquery.post/

    Try below modified code:

    function send_message(value){
        var data={txt:value};
          jQuery.post(ajaxurl, data, function(response) {
            alert(response);
          });
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配