简体   繁体   English

单击按钮时如何显示消息

[英]How to show a message when a button is clicked

I want to show the following message when the button below is clicked using jQuery当使用 jQuery 单击下面的按钮时,我想显示以下消息

<p class="msg-confirm" id="msgConf">
  Great! You got this. Let's continue.
</p>

Button:按钮:

<input type="button" value="Start" class="btn-start" id="exec">

This message is set as none in CSS:此消息在 CSS 中设置为无:

.msg-confirm{
display: none;

} }

I have this function that worked before on a similar context, but without the validation.我有这个 function 以前在类似的环境中工作过,但没有经过验证。 If the checkbox below is checked, I want this function working.如果选中下面的复选框,我希望这个 function 工作。

    $("#exec").click(function(){
    if($('#d3').is(':checked')){
        $("#msgConf").show('slow');
    }
});

Checkbox:复选框:

<input type="radio" name="image" id="d3" class="input-step1 aheadF1"/>

Let's make use of the simplicity of some of the new features of jQuery such as the .prop() method that will allow us to verify if a checkbox or radio button is checked.让我们利用 jQuery 的一些新功能的简单性,例如.prop() 方法,它允许我们验证是否选中了复选框或单选按钮。 For the purpose of this example, I switched the input to a checkbox since it is more appropriate UX/UI wise speaking, however, this property can be verified in both controls.出于本示例的目的,我将输入切换为复选框,因为它更适合 UX/UI 明智的说法,但是,可以在两个控件中验证此属性。 We will use the toggleClass() method of jQuery to toggle the class that hides the P tag and its content initially.我们将使用 jQuery 的toggleClass() 方法来切换最初隐藏 P 标签及其内容的 class。 I certainly hope this helps.我当然希望这会有所帮助。

Happy coding!快乐编码!

 $(document).ready(function () { $("#exec").click(function () { if ($('#d3').prop('checked')) { $("p").toggleClass("msg-confirm"); } else { alert("Please select the checkbox to display info."); } }); });
 .msg-confirm { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <p class="msg-confirm"> Great. You got this. Let's continue. </p> <input type="button" value="Start" class="btn-start" id="exec"> <input type="checkbox" name="image" id="d3" class="input-step1 aheadF1"/>

Try this尝试这个

$("#exec").on("click",function (){
    if($('#d3').is(':checked')){
       $("#msgConf").css("display","")
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM