【问题标题】:How can I specify a specific value in a column retrieved via php?如何在通过 php 检索的列中指定特定值?
【发布时间】:2014-10-18 05:34:57
【问题描述】:

我想添加一个关于“类型”列的 if 语句。 .attr('type') 后面有什么东西可以让我为类型指定一个特定的值吗?

$.get("map_process.php", function (data) {
            $(data).find("marker").each(function () {
                  var name      = $(this).attr('name');
                  var address   = '<p>'+ $(this).attr('address') +'</p>';
                  var type      = $(this).attr('type');

所以$(this).attr('type'); 正在加载我的表“类型”列值中的所有行。例如:
表格
姓名、地址、类型*
姓名1,地址1,类型A,
姓名2,地址2,类型B,
姓名 3,地址 3,类型 A,
等等

我如何才能“访问”“类型”列的实际值;例如。 $(this).attr('type').&lt;something&gt;('TypeA');

这可能吗?

Edit2:map_process.php 结束

// Select all the rows in the markers table
$query = "SELECT * FROM markers";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo 'description="' . parseToXML($row['description']) . '" ';

  echo '/>';
}

// End XML file
echo '</markers>';

?>

【问题讨论】:

  • 你能说得更具体点吗?你的问题不清楚。
  • 你的意思是标签的type:b, input, div ...?
  • 对不起,我只是将表格的每个值加载到谷歌地图中,​​并使用表格“类型”列中的值显示在信息框中。但是比如说,我只想将具有特定值(在表格内)的标记加载到我的地图中。
  • 显示map_process.php的部分结果。
  • 您可以将选中的复选框值发送到 map_process.php 并从表中获取选中的值并在 Map 中再次更新标记。

标签: javascript php jquery


【解决方案1】:

您有两个选项:

(1) 如果您可以完全控制所调用的 ajax 页面,最好的解决方案是:

"将选中的复选框值发送到 map_process.php 并获取 从表中选择值并在地图中再次更新标记。” - 克里什 R

//jQuery
var cbVal = "TYPEA"; //Assign with checkbox value's associated string
$.get("map_process.php?checkboxval=" + cbVal, function (data) 
{ 
    //do something with the data returned
});

//PHP
$query = "SELECT * FROM markers where type = '".$_GET["checkboxval"]."'";

(2) 如果您无法控制 ajax 调用的来源(例如来自第三方来源),或者您希望每次都返回所有标记,请考虑使用jQuery Filter 方法。

//jQuery
var cbVal = "TYPEA"; //Assign with checkbox value's associated string
$(data).filter(function(index,val) 
{
    return $(val).attr('type') === cbVal;
}).each(function () 
{
    //do something with the data returned
});

请参阅this JSFiddle 上选项 (2) 的示例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 2020-11-20
    • 2019-11-07
    相关资源
    最近更新 更多