【问题标题】:passing variables with $.ajax使用 $.ajax 传递变量
【发布时间】:2014-01-24 20:33:18
【问题描述】:

我有两个复选框,其中一些值如下:

<label for="hotel_boutique"><input id="hotel_boutique" name="hotel_boutique" value="test" type=checkbox />test</label><br />

3

我通过这样的 Ajax 调用获得这些值:

<script>
jQuery("input[type='checkbox']").change(function(){

if (jQuery('input#hotel_boutique').is(':checked')) {
    var hotel_boutique = jQuery("#hotel_boutique").map(function () {return this.value;}).get();
}else{
    var hotel_boutique = 'NULL';
    }
if (jQuery('input#hotel_stars').is(':checked')) {
    var hotel_stars = jQuery("#hotel_stars").map(function () {return this.value;}).get();
}else{
    var hotel_stars = 'NULL';
    }

var data = 'hotel_boutique="'+hotel_boutique+'"&hotel_stars="'+hotel_stars+'"';
jQuery.ajax({
    url: "processAjax.php",
    type: "GET",
    data: data,
    cache: false,
    beforeSend: function() {
        jQuery("#loading").show();
    },
    success: function(data, textStatus, XMLHttpRequest){
        jQuery("#content").html('');
        jQuery("#content").append(data);
        jQuery("#loading").hide();
    },
    error: function(MLHttpRequest, textStatus, errorThrown){
        alert(errorThrown);
    }

});
});
</script>

现在,当我在服务器端 (PHP) 中回显变量时,它会显示:

"test"

" 添加了谁以及为什么添加?我怎样才能删除它们?

我已经尝试过PHP函数preg_replace和其他东西。

请需要你的帮助...

【问题讨论】:

    标签: php jquery ajax variables parameter-passing


    【解决方案1】:

    你可以使用对象而不是字符串

    var data = {
        hotel_boutique: hotel_boutique,
        hotel_stars: hotel_stars
    };
    

    问题是数据字符串中的",你也可以试试

    var data = 'hotel_boutique='+hotel_boutique+'&hotel_stars='+hotel_stars;
    

    整体代码可以简化为

    jQuery("input[type='checkbox']").change(function () {
        var data = {
            hotel_boutique: $('#hotel_boutique:checked').val()||'NULL',
            hotel_stars: $('#hotel_stars:checked').val()||'NULL'
        };
        jQuery.ajax({
            url: "processAjax.php",
            type: "GET",
            data: data,
            cache: false,
            beforeSend: function () {
                jQuery("#loading").show();
            },
            success: function (data, textStatus, XMLHttpRequest) {
                jQuery("#content").html(data);
                jQuery("#loading").hide();
            },
            error: function (MLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    });
    

    【讨论】:

    • +1,唯一真正“正确”的答案,因为不需要对象符号。
    【解决方案2】:

    你需要传递键值对:

    data: {hotel_boutique: hotel_boutique,hotel_stars: hotel_stars},
    

    在本次通话中:

    jQuery.ajax({
        data: data,      // change here
    });
    

    【讨论】:

      【解决方案3】:

      应该是:

      jQuery.ajax({
          url: "processAjax.php",
          type: "GET",
          data: {
              hotel_boutique: hotel_boutique,
              hotel_stars : hotel_stars
           },
          cache: false,
          beforeSend: function() {
              jQuery("#loading").show();
          },
          success: function(data, textStatus, XMLHttpRequest){
              jQuery("#content").html('');
              jQuery("#content").append(data);
              jQuery("#loading").hide();
          },
          error: function(MLHttpRequest, textStatus, errorThrown){
              alert(errorThrown);
          }
      
      });
      

      【讨论】:

        猜你喜欢
        • 2023-01-24
        • 2016-08-31
        • 1970-01-01
        • 2020-01-23
        • 2018-06-30
        • 1970-01-01
        • 2011-08-18
        • 1970-01-01
        • 2013-07-27
        相关资源
        最近更新 更多