Javascript 我需要一种方法来识别当php生成所有按钮时按下的按钮
我正在使用php生成未定义数量的div,每个div的格式相同,但填充的信息不同。我的问题是我不知道如何识别被按下的按钮在哪个div中?我想知道被按下按钮的$row[“bar_name”]。i、 e.如果按了星期一,我还需要知道星期一按钮位于哪个$row[“bar_name”]内。我更改了div生成方式的格式,因为与在php中进行响应相比,它在html中更易于阅读。非常感谢您的帮助,谢谢Javascript 我需要一种方法来识别当php生成所有按钮时按下的按钮,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我正在使用php生成未定义数量的div,每个div的格式相同,但填充的信息不同。我的问题是我不知道如何识别被按下的按钮在哪个div中?我想知道被按下按钮的$row[“bar_name”]。i、 e.如果按了星期一,我还需要知道星期一按钮位于哪个$row[“bar_name”]内。我更改了div生成方式的格式,因为与在php中进行响应相比,它在html中更易于阅读。非常感谢您的帮助,谢谢 // how the div is being generated (slightly altered for
// how the div is being generated (slightly altered for easier readability)
<?php
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
//this is echoed as one string in my actual code
<div id='" . $row["bar_name"] . "'>
<h3 id='barTitle' class='bar'>" . $row["bar_name"] . "</h3>
<h6 id='subTitle'>". $row["hourStart"] . "-" . $row["hourEnd"] . " | " . $row["area"] . "</h6>
<table>
<tr>
<th class='dowb'> <button id='monday'> Monday </button></th>
<th class='dowb'> <button id='tuesday'> Tuesday </button></th>
<th class='dowb'> <button id='wednesday'> Wednesday </button></th>
<th class='dowb'> <button id='thursday'> Thursday </button></th>
<th class='dowb'> <button id='friday'> Friday </button></th>
<th class='dowb'> <button id='saturday'> Saturday </button></th>
<th class='dowb'> <button id='sunday'> Sunday </button></th>
<br>
</tr>
</table>
<br>
<table>
<tr>
<th class='barInfo'> <strong> Drinks: </strong></th>
<th class='barInfo'> <strong> Food: </strong></th>
</tr>
<tr>
<th class='barInfo' id="dowDrink"> Things to drink</th>
<th class='barInfo' id="dowFood"> Things to eat</th>
</tr>
</table>
<a href='" . $row["profile_page"] . "'> More Info </a>
<hr>
</div>
}
}
?>
//ajax (its the same code with the names switched out for monday-sunday)
$("#monday").click(function() {
var monday = true;
$.ajax({
type: "POST",
dataType: "html",
url: "barMapPhp.php",
data: {monday: monday},
success: function(result){
jQuery("#dowd").html(result);
}
});
});
// php code that ajax is sent to but im unsure how to proceed from here if i cant specify which $row["bar_name"] div that the button was in
if(isset($_POST["monday"])){
???
}
//div是如何生成的(稍微修改一下,以便于阅读)
//ajax(与周一周日的名称相同)
$(“#星期一”)。单击(函数(){
var周一=真;
$.ajax({
类型:“POST”,
数据类型:“html”,
url:“barmappp.php”,
数据:{星期一:星期一},
成功:功能(结果){
jQuery(“#dowd”).html(结果);
}
});
});
//ajax发送到的php代码,但如果我不能指定按钮所在的$row[“bar_name”]div,我不确定如何从这里开始
如果(isset($_POST[“星期一])){
???
}
如果您有多个星期一按钮,请使用类似于.monday
的类而不是id来访问按钮(您不应该在一个页面上有多个id相同的元素。要识别特定的按钮
bar\u name=”“
,并使用jQuery中的$(this.attr('bar\u name')
访问它。单击()
回调函数.parent()
方法查找按钮的父级
,并获取其id
(条形图名称)属性:
$('.monday').parent().parent().parent().parent().attr('id')您可以使用
.closest()
,在不知道html结构的情况下找到与给定选择器匹配的容器
此外,您根本不需要每天使用多个函数,您可以根据给定设置的按钮名称计算一天。但是请注意,id
属性应该是唯一的,即使这样做有效,您也应该修复它,可能改为使用data
属性
如果你有很多元素,你应该使用
$(文档)。在('click','button',function()上{
设div=this.closest('div').id;
让数据={};
数据[this.id]=true;
log(`clicked${this.id}在div${div}`中);
控制台日志(数据);
});
酒吧名称
星期一
星期二
星期三
星期四
星期五
星期六
星期日
酒吧名称
星期一
星期二
星期三
星期四
星期五
星期六
星期日