通过jQuery的$ .post的问题的变量作为参数 [英] Problems passing jQuery $.post a variable as a parameter

查看:453
本文介绍了通过jQuery的$ .post的问题的变量作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个名字发送:值对jQuery中使用$。员额对象,其中两个名称和值是变量的PHP脚本。我发出的变量在FF的控制台上,所以我知道他们正在设置正确,但是当我查看头被发送,只有变量的值被正确评估,这个名字被发送作为变量的文本字符串名称。下面是函数在其全部:

I am trying to send a name:value pair to a php script using the $.post object in jQuery where both name and value are variables. I have sent the variables to the console in FF, so I know they are being set correctly, but when I view the headers being sent, only the value variable is being evaluated correctly, the name is being sent as a literal string of the variable name. Here is the function in its entirely:

$(document).ready(function() {
  $('#formS form fieldset > input').blur(function() {
    if($(this).val()=='' && $(this).prev().is('img')) {
     $(this).prev().remove();
     return;
    }

    if((($(this).is('#formS form fieldset > input[id*=mail]')) ||
    ($(this).is('#formS form fieldset > input[id*=sername]'))) &&
    ($(this).val()!="")) {
      var email_field = $(this);
      if(!$(this).prev().is('img')){
      $('<img src="" alt="" style="width: 16px; height: 16px;" />').insertBefore(this);
      }
      var type = ($(this).is('input[id*=sername]'))?'username':'email';
      var value = $(this).val();
      $.post('checkemail.php5', {type:value}, function(data) {
                if(data == "free") {
         email_field.prev().attr('src','/images/greentick.png').attr('alt','available');
        } else if(data == "taken") {
         email_field.prev().attr('src','/images/redcross.png').attr('alt','taken');
        } else {
          console.log('hmmm'+data);
        }
        });
        }
        });
});

因此​​,所发送的报头都是文字串键入,和任何的的计算结果为。任何人有任何想法,为什么?

So, the headers being sent are the literal string type, and whatever value evaluates to. Anybody have any ideas why?

推荐答案

由于语法{}采用一组名称:值,其中名字成为对象的属性。这是文字的,不能是一个变量,如你所见。使用这样的:

Because the syntax {} takes a set of name:value where the names become the property on the object. This is a literal and cannot be a variable, as you see. Use this:

var params = {};
params[type] = value;

和传递PARAMS而不是{类型:值}

and pass params instead of {type:value}

这篇关于通过jQuery的$ .post的问题的变量作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆