javascript - 切换按钮似乎不起作用?
问题描述
嗨,我确定我已经按照教程中的步骤正确编写了此代码。我希望单击#booking 按钮,它应该“切换”#btn 元素消失。本质上是一个包含汽车信息的框,当用户单击“单击此处预订”时,它应该打开表单。但由于某种原因,它没有。我认为这可能与 CSS 有关,但我不确定。
对此有什么建议吗?
<div class="form-wrapper">
<form action="#">
<label for="name">Name*</label>
<input placeholder="Your Name" class="full" type="text"id="name">
<br>
<label for="email">Email</label>
<input placeholder="Your Email" class="full" type="text"id="name">
<br>
<label for="hire-start-date">Hire Start Date</label>
<input type="date" id="depart">
<br>
<label for="hire-end-date">Hire End Date</label>
<input type="date" id="depart">
<br>
<button class="book-now">Book Now</button>
</form>
</div>
<div class="car-info" id="btn">
<button id="booking-button">Click Here to Book</button>
</div>
</div>
$(document).ready(function() {
$('#booking-button').on('click', () =>
{ $('#btn').toggle(); });
});
解决方案
现在,当用户单击时表单出现Click Here to Book
,而此按钮消失。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-wrapper">
<form action="#" id="car-info" style="display: none;">
<label for="name">Name*</label>
<input placeholder="Your Name" class="full" type="text"id="name">
<br>
<label for="email">Email</label>
<input placeholder="Your Email" class="full" type="text"id="name">
<br>
<label for="hire-start-date">Hire Start Date</label>
<input type="date" id="depart">
<br>
<label for="hire-end-date">Hire End Date</label>
<input type="date" id="depart">
<br>
<button class="book-now">Book Now</button>
</form>
</div>
<div id="btn">
<button id="booking-button">Click Here to Book</button>
</div>
<script>
$(document).ready(function() {
$('#booking-button').on('click', () => {
$('#car-info').toggle();
$('#btn').toggle();
});
});
</script>
推荐阅读
- java - neo4j java 程序示例因 NoClassDefFoundError 而失败
- actions-on-google - Dialogflow,Google 助理:出现错误“必须设置 MalformedResponse 'final_response'”
- html - 悬停时的CSS动画第二个div
- firebase-cloud-messaging - Firebase 云消息传递在应用程序运行时不通知
- linux - 将“模块”别名(调用 modulecmd)从 tcsh 调整为 bash
- javascript - 更改表单控件Angular中的数据
- java - 分块成多个文件时断线
- javascript - JavaScript,如何使用 moment.js 创建日期差异
- html - 如果使用 mso 页脚,Word HTML 分页符将不起作用
- javascript - Web 套接字、socket.io 或其他替代方案