javascript - 第三次打开时只弹出预期的功能?
问题描述
我有一个弹出窗口,我正在制作它的预期功能是使用交互式日历打开它允许用户使用 jquery datepicker 输入开始日期和结束日期。但是目前在页面加载时,我必须打开和关闭两次弹出窗口才能按预期运行我想知道为什么会发生这种情况/如何解决它?这是代码
有问题的网页
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<title> {(BnB)} Market </title>
</head>
<div id= 'user_listings'>
</div>
<body>
Market BnB
</body>
<a href = '/make_listings' > Looking to show your own listing? </a>
<div id = 'table'>
<table>
<tr>
<h1> BnB-able properties </h1>
</tr>
<% @BnB.each do |record| %>
<tr>
<td class ='new_BnB'><%= record['address'] %> </td>
<td class ='code_BnB'> | <%= record['post_code']%> </td>
<br>
<a id="OpenDialog" href="#"> <img src = <%= record['photo'] %> width="300" height="200" alt='test' >
</a>
<div id="dialog" title="Property Info" style="display: none;" >
<p>
<p>Start date: <input type="text" id="datepicker"> End date: <input type="text" id="datepicker2"></p>
<input type = "submit" value='Submit'>
</p>
</div>
</tr>
</table>
<% end %>
<head>
<head>
<title></title>
</head>
<body>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/redmond/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
</body>
<%# <title>jQuery UI Datepicker - Default functionality</title> %>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<script src="interface.js"></script>
</div>
接口.js
$(document).ready(function() {
$("#OpenDialog").click(function () {
$("#dialog").dialog({modal: true, height: 500, width: 700 });
});
});
$( function() {
$( "#datepicker" ).datepicker();
} );
$( function() {
$( "#datepicker2" ).datepicker();
} );
** @BnB 的值是什么**
require 'pg'
class BnBControl
attr_accessor :connection
def self.all
@connection = PG.connect(dbname: 'bnb')
listings = @connection.exec("SELECT * FROM properties")
end
------
class BnB < Sinatra::Base
enable :sessions
get '/' do
#if session[:user]
@BnB = BnBControl.all
erb :index_logged_in
# else
# erb :index
# end
end
这些是单独的文件,只是包含在同一个代码围栏中
解决方案
您使用document.ready
的以及$(function()
哪些是相同的,不需要多次使用。$(document).ready
另外,在弹出点击事件处理程序之前调用 datepicker 函数,这应该适合你。
试试下面
$(document).ready(function() {
$( "#datepicker" ).datepicker();
$( "#datepicker2" ).datepicker();
$("#OpenDialog").click(function () {
$("#dialog").dialog({modal: true, height: 500, width: 700 });
});
});
推荐阅读
- asp.net-mvc - ASP .NET MVC 500 内部服务器错误 - 在获取和发布时 - 相同的应用程序/不同的服务器
- mysql - SQL - order by is breaking my query when there is no reviews
- c# - 在 .NET 2.0 下工作的文件下载现在只能在 .NET4.6 下工作
- solr - org.apache.curator.ConnectionState 连接字符串超时
- c++ - 具有 Boost::Fiber 的多个共享工作池
- teradata - 仅保留 teradata 中特定数据部分的值
- go - os.PathError 没有实现错误
- javascript - 未找到 React Webpack 开发服务器 main.js
- bash - sed 命令:使用 \n 字符搜索和替换变量
- java - 在 ImageView 上的鼠标单击事件上获取 RGB 像素值