gpt4 book ai didi

javascript - jQuery 无法在属性选择器中的空格后读取单词

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:33 26 4
gpt4 key购买 nike

这是 Carto 中下拉搜索菜单/过滤器的脚本。在设置 sql 查询之前,该脚本似乎可以正常工作。每当其中一个属性名称中有空格时,脚本只会找到第一个单词,因此数据库中没有匹配项。像“San Deigo”只会返回“San”。变量“input”的值只是第一个词。如果属性没有空格,则脚本可以正常工作。

这可能是 select to 的问题还是有其他问题?

<!DOCTYPE html>
<html>
<head>
<title>Searchbox + Dropdown menu + Zoom + CARTO.js</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" href="https://cartodb.com/assets/favicon.ico" />

<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<style>
html, body, #map {
height: 100%;
padding: 0;
margin: 0;
}
#wraper-city-selector {
position: absolute;
top: 20px;
left: 20px;
}
.js-city-selector {
width: 175px;
}

</style>

</head>
<body>

<!-- map div -->
<div id="map"></div>

<!-- selector div -->
<div id="wraper-city-selector">
<select class="js-city-selector">
<option value="#" selected disabled>Select a city</option>
</select>
</div>

<!-- legend div -->
<div class='cartodb-legend category'>
<div class="legend-title" style="color:#284a59">Cities</div>
<ul>
<li><div class="bullet" style="background-color:#5F4690"></div>Populated Place</li>
<li><div class="bullet" style="background-color:#1D6996"></div>Admin. 1 Capital</li>
<li><div class="bullet" style="background-color:#38A6A5"></div>Admin. 0 Capital</li>
<li><div class="bullet" style="background-color:#0F8554"></div>Admin. 1 region Capital</li>
<li><div class="bullet" style="background-color:#73AF48"></div>Scientific Station</li>
<li><div class="bullet" style="background-color:#EDAD08"></div>Admin. O region Capital </li>
<li><div class="bullet" style="background-color:#E17C05"></div>Admin. 0 Capital Alt.</li>
<li><div class="bullet" style="background-color:#CC503E"></div>Historic Place</li>
<li><div class="bullet" style="background-color:#94346E"></div>Meteorological Station</li>
</ul>
</div>

<script type="text/cartocss" id="style">
#layer {
marker-width: 10;
marker-fill: ramp([featurecla], (#5F4690, #1D6996, #38A6A5, #0F8554, #73AF48, #EDAD08, #E17C05, #CC503E, #94346E), ("Populated place", "Admin-1 capital", "Admin-0 capital", "Admin-1 region capital", "Scientific station", "Admin-0 region capital", "Admin-0 capital alt", "Historic place", "Meteorological Station"), "=");
marker-fill-opacity: 0.7;
marker-allow-overlap: true;
marker-line-width: 0;
}
</script>

<script type="text/sql" id="query">
SELECT * FROM populated_places WHERE pop_max > 700000
</script>

<script>
function main() {

// get selector
var selector = $(".js-city-selector");

// get styles & query
style = $("#style").text(),
query = $("#query").text(),

// declare map variable
map = L.map('map', {
zoomControl: false,
center: [41, -3],
zoom: 3
});

// add basemap
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="http://cartodb.com/attributions">CARTO</a>'}).addTo(map);

// add cartodb layer
cartodb.createLayer(map, {
user_name: 'ramirocartodb',
type: 'cartodb',
sublayers: [{
sql: query,
cartocss: style,
}]
}).addTo(map)
.done(function(layer){

// declare sublayer variable
var cityLayer = layer.getSubLayer(0);

// populate selector with city names from sublayer data
var sql = new cartodb.SQL({ user: 'ramirocartodb' });
sql.execute(query)
.done(function(data) {
for (var i = 0; i < data.total_rows; i++) {

var name = data.rows[i].name;

selector.append("<option value=" + name + ">" + name + "</option>");
}
});

selector.select2();

// filter & zoom cities when selecting an option from selector
selector.change(function(){

var input = $( ".js-city-selector option:selected" ).val();

console.log("You have selected " + input + ".");

cityLayer.setSQL(query + " AND name ilike '" + input + "'");

sql.getBounds(query + " AND name ilike '" + input +
"'")
.done(function(bounds) {

map.fitBounds(bounds);
map.setZoom(16);

});


});
});
}
window.onload = main;
</script>

</body>
</html>

最佳答案

问题所在是

selector.append("<option value=" + name + ">" + name + "</option>");

您需要将属性用引号引起来

selector.append('<option value="' + name + '">' + name + '</option>');

var name = "San Deigo";
$('select').append('<option value="' + name + '">' + name + '</option>');

name = "los angeles";
$('select').append('<option value="' + name + '">' + name + '</option>');


$('select').on('change', function() {
console.log($(this).val())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
</select>

关于javascript - jQuery 无法在属性选择器中的空格后读取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47096201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com