Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 替换空格或对其进行编码_Jquery - Fatal编程技术网

Jquery 替换空格或对其进行编码

Jquery 替换空格或对其进行编码,jquery,Jquery,我有以下Jquery调用 $("#btnfindAddress").click(function() { var dataString = ''; //built the data string that will be sent with ajax dataString += 'business_name='+$('#company').val()+'&'; dataString += 'business_city='+$('#ci

我有以下Jquery调用

$("#btnfindAddress").click(function() {
   var dataString = '';
        //built the data string that will be sent with ajax
        dataString += 'business_name='+$('#company').val()+'&';
        dataString += 'business_city='+$('#city').val()+'&';
        dataString += 'business_country='+$('#country').val()+'&';
        dataString += 'business_zipcode='+$('#zipcode').val();
$.ajax({
type: "GET",
url: "/locations/search",
contentType: "application/text; charset=utf-8",
data: dataString, 
success: function(data){ 
    var text_result="";
    text_result="<table  id=\"thetable\"><tbody>";
    $.each(data,function(index,value){
          text_result+="<tr>";
          text_result+="<td>"+value.name+"</td>";
          text_result+="<td>"+value.address+"</td>";
          text_result+="<td>"+value.zipcode+"</td>";
          text_result+="<td><a name="+ value.name+" address="+value.address+" city="+value.city+"href=>Select</a></td>";
          text_result+="</tr>";
    });
          $('#locations').html(text_result);
}
});
 return false; 
});
$(“#btnfindAddress”)。单击(函数(){
var数据字符串=“”;
//构建将使用ajax发送的数据字符串
dataString+='business_name='+$('#company').val()+'&';
dataString+='business_city='+$('#city').val()+'&';
数据字符串+='business_country='+$('#country').val()+'&';
dataString+='business_zipcode='+$('#zipcode').val();
$.ajax({
键入:“获取”,
url:“/locations/search”,
contentType:“应用程序/文本;字符集=utf-8”,
数据:dataString,
成功:函数(数据){
var text_result=“”;
text_result=“”;
$.each(数据、函数(索引、值){
文本_结果+=“”;
text_result+=“”+value.name+“”;
text_result+=“”+value.address+“”;
text_result+=“”+value.zipcode+“”;
文本_结果+=“”;
文本_结果+=“”;
});
$('#locations').html(文本结果);
}
});
返回false;
});
它生成以下html

<a href="" angeles="" city="Los" website="null" zipcode="90007"  st="" hoover="" s="" address="3303" coffee="" name="Starbucks">Select</a>

这些值按空间分割

应该是的

    <a href="" city="Los angeles"   address="3303 S Hoover st" coffee="" name="Starbucks">Select</a>

我怎样才能解决这个问题


谢谢

您需要在生成的html中添加引号:

text_result+="<td><a name=\""+ value.name+"\" address=\""+value.address+"\" city=\""+value.city+"\" href>Select</a></td>";
text_结果+=”;

您需要在生成的html中添加引号:

text_result+="<td><a name=\""+ value.name+"\" address=\""+value.address+"\" city=\""+value.city+"\" href>Select</a></td>";
text_结果+=”;

您不应该使用字符串操作来执行此操作。如果执行以下操作,jQuery将正确处理编码

var $table = $('<table>').attr('id','theTable');
$.each(data,function(index,value) {
    $table.append(
       $('<tr>')
         .append($('<td>').text(value.name))
         .append($('<td>').text(value.address))
         .append($('<td>').text(value.zipcode))
         .append($('<td>').append(
           $('<a>').attr('name',value.name).attr('address',value.address)
         )
    );
});
$('#locations').html($('<div>').append($table).html());
var$table=$('').attr('id','theTable');
$.each(数据、函数(索引、值){
$table.append(
$('')
.append($('').text(value.name))
.append($('').text(value.address))
.append($('').text(value.zipcode))
.append($('').append(
$('').attr('name',value.name).attr('address',value.address)
)
);
});
$('#locations').html($('').append($table.html());

更好的方法是使用诸如EJS或Moustache之类的模板方法。

您不应该通过字符串操作来执行此操作。如果您执行以下操作,jQuery将正确处理编码

var $table = $('<table>').attr('id','theTable');
$.each(data,function(index,value) {
    $table.append(
       $('<tr>')
         .append($('<td>').text(value.name))
         .append($('<td>').text(value.address))
         .append($('<td>').text(value.zipcode))
         .append($('<td>').append(
           $('<a>').attr('name',value.name).attr('address',value.address)
         )
    );
});
$('#locations').html($('<div>').append($table).html());
var$table=$('').attr('id','theTable');
$.each(数据、函数(索引、值){
$table.append(
$('')
.append($('').text(value.name))
.append($('').text(value.address))
.append($('').text(value.zipcode))
.append($('').append(
$('').attr('name',value.name).attr('address',value.address)
)
);
});
$('#locations').html($('').append($table.html());

更好的方法是使用EJS或Moustache之类的模板方法。

您不应该手工构建
数据
组件。只需使用对象文本:
$.ajax({…,数据:{foo:'bar',business_city:$(“#city”).val()})
它生成以下html
不,它不生成。这是您的DOM explorer告诉您的。不是实际的html。查看源代码,您就会看到发生了什么。您不应该手动构建
数据组件。只需使用对象文本:
$.ajax({…,数据:{foo:'bar',business#city:$(“#city”).val()}})
它生成以下html
不,它不生成。这是您的DOM资源管理器告诉您的。不是实际的html。查看源代码,您就会看到发生了什么。我更喜欢这种方法。它很干净。但由于某些原因,$table.append不起作用。可能缺少一些内容。我不知道a中返回的数据JAX。这种方法非常基本,经常使用,但使用JavaScript模板引擎更好。我鼓励考虑这一点,因为它将节省大量时间并提供更干净的方法。EJS是我个人的首选。我更喜欢这种方法。它很干净。但出于某种原因,$table.append不起作用..ma我可能遗漏了一些东西。我不知道AJAX中返回的数据。这种方法非常基本,使用非常频繁,但使用JavaScript模板引擎更好。我鼓励考虑这一点,因为它将节省大量时间,并提供更干净的方法。EJS是我个人最喜欢的方法。