0

我有一个像复选框一样的自定义 div 来设置条件,而且我有一个弹出窗口,其中包含一个按钮来触发条件。

但是,我不知道为什么,当从弹出的按钮触发自定义div的点击事件时。它不起作用。

如果我用弹出窗口外的按钮触发它,它工作正常。

这是它的演示: https ://jsfiddle.net/yusrilmaulidanraji/ayLamku6/8/

单击“弹出触发器”按钮时,应更改绿色矩形的颜色。

$(".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>

4

2 回答 2

0

最后,您需要替换此代码。您没有包含按钮的 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>

于 2017-05-17T11:45:29.287 回答
0

您没有向弹出窗口中的提交按钮添加 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>

于 2017-05-17T11:38:34.003 回答