弹出窗口内的按钮不会触发弹出窗口外的点击事件
Button inside popup doesn't trigger click event outside the popup
我有一个自定义 div 像一个复选框来设置一个条件,我还有一个弹出窗口包含一个按钮来触发条件。
但是不知道为什么,在弹出的按钮触发自定义div的点击事件的时候。它不起作用。
如果我用弹出窗口外的按钮触发,效果很好。
这是它的演示:
https://jsfiddle.net/yusrilmaulidanraji/ayLamku6/8/
单击 "Pop Up Trigger" 按钮时应更改绿色矩形的颜色。
$(".simon-toggle-accepted, .simon-toggle-declined").on("click", function () {
var value = $(this).data("value");
var tr = $(this).closest("tr");
if (value == "Accepted") {
if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
$(this).css("background-color", "green");
$(this).siblings("div").css("background-color", "white");
} else {
$(this).css("background-color", "white");
$(this).siblings("div").css("background-color", "white");
}
}
else if (value == "Declined") {
if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
$(this).css("background-color", "red");
$(this).siblings("div").css("background-color", "white");
} else {
$(this).css("background-color", "white");
$(this).siblings("div").css("background-color", "white");
}
}
});
$("#trigger").on("click",function (e) {
e.preventDefault();
$(".simon-toggle-accepted").trigger("click");
console.log("Test Trigger");
});
$("#dlg-opt-submit").on("click",function (e) {
e.preventDefault();
console.log("Test Popup Trigger");
$(".simon-toggle-accepted").trigger("click");
console.log($(".simon-toggle-accepted").length);
$("#popupLogin").popup("close");
});
.simon-toggle {
width: 90px;
height: 30px;
}
.simon-toggle-accepted {
background-color: white;
border: 1px solid green;
}
.simon-toggle-declined {
background-color: white;
border: 1px solid red;
}
.simon-toggle-ignored, .simon-toggle-accepted, .simon-toggle-declined {
width: 30px;
height: 30px;
display: inline-block;
border-radius: 2px;
margin-left: 0.5px;
margin-right: 0.5px;
}
<link href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
<div class="simon-toggle">
<div class="simon-toggle-accepted" data-value="Accepted"></div>
<div class="simon-toggle-declined" data-value="Declined"></div>
</div>
<button id="trigger">Trigger</button>
<a href="#popupLogin" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">Pop Up</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form>
<div style="padding:10px 20px;">
<button type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b ">Pop Up Trigger</button>
</div>
</form>
</div>
您没有向弹出窗口中的提交按钮添加 id 属性...
$(".simon-toggle-accepted, .simon-toggle-declined").on("click", function () {
var value = $(this).data("value");
var tr = $(this).closest("tr");
if (value == "Accepted") {
if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
$(this).css("background-color", "green");
$(this).siblings("div").css("background-color", "white");
} else {
$(this).css("background-color", "white");
$(this).siblings("div").css("background-color", "white");
}
}
else if (value == "Declined") {
if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
$(this).css("background-color", "red");
$(this).siblings("div").css("background-color", "white");
} else {
$(this).css("background-color", "white");
$(this).siblings("div").css("background-color", "white");
}
}
});
$("#trigger").on("click",function (e) {
e.preventDefault();
$(".simon-toggle-accepted").trigger("click");
console.log("Test Trigger");
});
$("#dlg-opt-submit").on("click", function (e) {
e.preventDefault();
console.log("Test Popup Trigger");
$(".simon-toggle-accepted").trigger("click");
console.log($(".simon-toggle-accepted").length);
$("#popupLogin").popup("close");
});
.simon-toggle {
width: 90px;
height: 30px;
}
.simon-toggle-accepted {
background-color: white;
border: 1px solid green;
}
.simon-toggle-declined {
background-color: white;
border: 1px solid red;
}
.simon-toggle-ignored, .simon-toggle-accepted, .simon-toggle-declined {
width: 30px;
height: 30px;
display: inline-block;
border-radius: 2px;
margin-left: 0.5px;
margin-right: 0.5px;
}
<link href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
<div class="simon-toggle">
<div class="simon-toggle-accepted" data-value="Accepted"></div>
<div class="simon-toggle-declined" data-value="Declined"></div>
</div>
<button id="trigger">Trigger</button>
<a href="#popupLogin" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">Pop Up</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form>
<div style="padding:10px 20px;">
<button type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b " id="dlg-opt-submit">Pop Up Trigger</button><!-- this was missing id="dlg-opt-submit" -->
</div>
</form>
</div>
最后你需要替换这段代码。您没有包含按钮的 ID。
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form>
<div style="padding:10px 20px;">
<button id="dlg-opt-submit" type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b ">Pop Up Trigger</button>
</div>
</form>
</div>
我有一个自定义 div 像一个复选框来设置一个条件,我还有一个弹出窗口包含一个按钮来触发条件。
但是不知道为什么,在弹出的按钮触发自定义div的点击事件的时候。它不起作用。
如果我用弹出窗口外的按钮触发,效果很好。
这是它的演示: https://jsfiddle.net/yusrilmaulidanraji/ayLamku6/8/
单击 "Pop Up Trigger" 按钮时应更改绿色矩形的颜色。
$(".simon-toggle-accepted, .simon-toggle-declined").on("click", function () {
var value = $(this).data("value");
var tr = $(this).closest("tr");
if (value == "Accepted") {
if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
$(this).css("background-color", "green");
$(this).siblings("div").css("background-color", "white");
} else {
$(this).css("background-color", "white");
$(this).siblings("div").css("background-color", "white");
}
}
else if (value == "Declined") {
if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
$(this).css("background-color", "red");
$(this).siblings("div").css("background-color", "white");
} else {
$(this).css("background-color", "white");
$(this).siblings("div").css("background-color", "white");
}
}
});
$("#trigger").on("click",function (e) {
e.preventDefault();
$(".simon-toggle-accepted").trigger("click");
console.log("Test Trigger");
});
$("#dlg-opt-submit").on("click",function (e) {
e.preventDefault();
console.log("Test Popup Trigger");
$(".simon-toggle-accepted").trigger("click");
console.log($(".simon-toggle-accepted").length);
$("#popupLogin").popup("close");
});
.simon-toggle {
width: 90px;
height: 30px;
}
.simon-toggle-accepted {
background-color: white;
border: 1px solid green;
}
.simon-toggle-declined {
background-color: white;
border: 1px solid red;
}
.simon-toggle-ignored, .simon-toggle-accepted, .simon-toggle-declined {
width: 30px;
height: 30px;
display: inline-block;
border-radius: 2px;
margin-left: 0.5px;
margin-right: 0.5px;
}
<link href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
<div class="simon-toggle">
<div class="simon-toggle-accepted" data-value="Accepted"></div>
<div class="simon-toggle-declined" data-value="Declined"></div>
</div>
<button id="trigger">Trigger</button>
<a href="#popupLogin" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">Pop Up</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form>
<div style="padding:10px 20px;">
<button type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b ">Pop Up Trigger</button>
</div>
</form>
</div>
您没有向弹出窗口中的提交按钮添加 id 属性...
$(".simon-toggle-accepted, .simon-toggle-declined").on("click", function () {
var value = $(this).data("value");
var tr = $(this).closest("tr");
if (value == "Accepted") {
if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
$(this).css("background-color", "green");
$(this).siblings("div").css("background-color", "white");
} else {
$(this).css("background-color", "white");
$(this).siblings("div").css("background-color", "white");
}
}
else if (value == "Declined") {
if ($(this).css("background-color") == "rgb(255, 255, 255)") { //white
$(this).css("background-color", "red");
$(this).siblings("div").css("background-color", "white");
} else {
$(this).css("background-color", "white");
$(this).siblings("div").css("background-color", "white");
}
}
});
$("#trigger").on("click",function (e) {
e.preventDefault();
$(".simon-toggle-accepted").trigger("click");
console.log("Test Trigger");
});
$("#dlg-opt-submit").on("click", function (e) {
e.preventDefault();
console.log("Test Popup Trigger");
$(".simon-toggle-accepted").trigger("click");
console.log($(".simon-toggle-accepted").length);
$("#popupLogin").popup("close");
});
.simon-toggle {
width: 90px;
height: 30px;
}
.simon-toggle-accepted {
background-color: white;
border: 1px solid green;
}
.simon-toggle-declined {
background-color: white;
border: 1px solid red;
}
.simon-toggle-ignored, .simon-toggle-accepted, .simon-toggle-declined {
width: 30px;
height: 30px;
display: inline-block;
border-radius: 2px;
margin-left: 0.5px;
margin-right: 0.5px;
}
<link href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
<div class="simon-toggle">
<div class="simon-toggle-accepted" data-value="Accepted"></div>
<div class="simon-toggle-declined" data-value="Declined"></div>
</div>
<button id="trigger">Trigger</button>
<a href="#popupLogin" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">Pop Up</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form>
<div style="padding:10px 20px;">
<button type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b " id="dlg-opt-submit">Pop Up Trigger</button><!-- this was missing id="dlg-opt-submit" -->
</div>
</form>
</div>
最后你需要替换这段代码。您没有包含按钮的 ID。
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form>
<div style="padding:10px 20px;">
<button id="dlg-opt-submit" type="submit" class="ui-btn ui-corner-all ui-shadow ui-btn-b ">Pop Up Trigger</button>
</div>
</form>
</div>