简体   繁体   English

单击后不显示警报

[英]Alert not show after click

I have this code:我有这个代码:

<div class="form-group">
    <div class="form-group has-feedback">
        <input required="required" name="query" type="text" class="form-control indexInputSzukaj" id="inputValidation" placeholder="Znajdź" value="">
        <span class="glyphicon glyphicon-search form-control-feedback glyphiconColor showActionAfterClick"></span>
    </div>
</div>

Jquery查询

$(document).ready(function() {
    $('.showActionAfterClick').click(function() {
        alert('ok');
    });
});

This code was created in bootstrap.此代码是在引导程序中创建的。

I need to show alert box after click showActionAfterClick class, but it's not working.我需要在单击showActionAfterClick类后显示警告框,但它不起作用。

How can I repair it?我该如何修复它?

You have a space between your class identifier ( . ) and your classname showActionAfterClick :您的类标识符 ( . ) 和类名showActionAfterClick之间有一个空格:

 $(document).ready(function() { $('.showActionAfterClick').click(function() { alert('ok'); }); });
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <div class="form-group "> <div class="form-group has-feedback "> <input required="required" name="query" type="text" class="form-control indexInputSzukaj" id="inputValidation" placeholder="Znajdź" value=""> <span class="glyphicon glyphicon-search form-control-feedback glyphiconColor showActionAfterClick">Text to see this button</span> </div> </div>

In this case, this happend because the span is an inline element and its width size depends of its content.在这种情况下,发生这种情况是因为 span 是一个内联元素,其宽度大小取决于其内容。 So the span element have not width and you can't click it and trigger the alert (besides the bad syntax in your jquery query selector).所以 span 元素没有宽度,你不能点击它并触发警报(除了你的 jquery 查询选择器中的错误语法)。 The @Jack Bashford example works because the span element has text inside and this provides the width to that element. @Jack Bashford 示例之所以有效,是因为 span 元素内部有文本,这为该元素提供了宽度。

<div class="form-group ">
<div class="form-group has-feedback ">
<input required="required" name="query" type="text" class="form-control indexInputSzukaj" id="inputValidation" placeholder="Znajdź" value="">
<span id ="showActionAfterClick" class="glyphicon glyphicon-search form-control-feedback glyphiconColor">Click ME!</span>
</div>
</div>



$(document).ready(function() {
    $('#showActionAfterClick').click(function() {
            alert('ok');
        });
});

add id in your span tag instead of class and replace .在您的 span 标签中添加 id 而不是 class 并替换 . with # and try now用 # 立即尝试

Use this用这个

<div class="form-group ">
<div class="form-group has-feedback ">
<input required="required" name="query" type="text" class="form-control indexInputSzukaj" id="inputValidation" placeholder="Znajdź" value="">
<span onclick="test()" class="glyphicon glyphicon-search form-control-feedback glyphiconColor"></span>
</div>
</div>


<script>
function test(){
alert("ok");
}
</script>

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

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