pey*_*y22 0 php variables ajax jquery parameter-passing
我有两个复选框,其中包含一些像这样的值:
<label for="hotel_boutique"><input id="hotel_boutique" name="hotel_boutique" value="test" type=checkbox />test</label><br />
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
现在,当我回显服务器端(PHP)中的变量时,它显示:
"test"
Run Code Online (Sandbox Code Playgroud)
谁和为什么被"
添加?我该如何删除它们?
我已经尝试过PHP函数preg_replace
和其他东西.
请你帮忙......
您可以使用对象而不是字符串
var data = {
hotel_boutique: hotel_boutique,
hotel_stars: hotel_stars
};
Run Code Online (Sandbox Code Playgroud)
问题是"
在数据字符串中,您也可以尝试
var data = 'hotel_boutique='+hotel_boutique+'&hotel_stars='+hotel_stars;
Run Code Online (Sandbox Code Playgroud)
整体代码可以简化为
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);
}
});
});
Run Code Online (Sandbox Code Playgroud)