javascript - JQuery Popup 仅适用于一个项目

标签 javascript php jquery mysql popup

我的表中至少有 10 个条目来自 mysql 数据库。每行都有一个按钮。单击该按钮时,将显示一个带有文本字段的弹出窗口。这个想法是在弹出窗口中输入该项目的出价。

该脚本正在运行,但仅适用于第一个元素,不适用于其余元素。我怎样才能动态地做到这一点?

代码如下:

$(document).ready(function(){
	$("#show").click(function() {
	  $("#popup").show();
	});

	$("#close, #submit").click(function() {
	  $("#popup").hide();
	});
});
#popup {
  position: relative;
  z-index: 100;
  padding: 10px;
}

.content {
  position: absolute;
  z-index: 10;
  background: #ccc;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #000;
  z-index: 5;
  opacity: 0.4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="popup" style="display: none;">
    <div class="overlay"></div>
    <div class="content">
      <header>
        <div id="close">✖</div>
      </header>
      <form name="form1" method="post" action="bid.php">
        <fieldset>
          <label for="bid">Bid</label>
		  <input type="text" name="bids[]" id="bids[]" size="8"/>
          <input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
        </fieldset>
		<footer>
			<button type="button" id="submit">Bid Now</button>
      </footer>
      </form>
    </div>
  </div>

 /*This is how I trigger the popup. It is an example for one item but all the rest have the same composition*/

<td><button id="show"><img src="pictures/bidIcon.png" width="30" height="30"></button></td>

最佳答案

Id 每个元素都需要唯一,这就是您代码中的问题。像这样将 id 转换为 class:-

$(document).ready(function(){
    $(".show").click(function() {
      $("#popup").show();
    });

    $("#close, #submit").click(function() {
      $("#popup").hide();
    });
});

按钮代码如下:-

<td><button class="show"><img src="pictures/bidIcon.png" width="30" height="30"></button></td>

工作片段:- https://jsfiddle.net/3u2jL2rh/

关于javascript - JQuery Popup 仅适用于一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50464752/

相关文章:

javascript - 从 redis 命令返回的数组中获取数据

php - 更改整个应用程序的默认 URL 或路径?

php - PHP 插入语句和 MYSQL 数据库的编码问题

jquery - 使用jquery自动将class更改为id

javascript - jQuery/JavaScript 在线投资组合表单 - 打印样式表

javascript - 如何调用 Angular Controller ?

javascript - 需要对 javascript 中的所有字段发出警报

php - 将 android JSONObject 发布到 PHP 的问题

javascript - jquery $.when - 有什么方法可以阻止 .fail 提前触发吗?

javascript - 使用 PHP 和 AJAX 获取页面名称而不是页码