找到你要的答案

Q:Event attached to button JQUERY

Q:附加到jQuery事件按钮

HTML Code:

<div class="row">
        <div class="col-sm-1 col-md-9"></div>
        <div class="col-sm-10 col-md-3">
            <div class="login-form">
                <form role="form" method="post">
                    <div class="form-group">
                        <label for="nick">Nick:</label>
                        <input class="form-control" type="text" id="nick-log" />
                        <span></span>
                    </div>
                    <div class="form-group">
                        <label for="password">Hasło</label>
                        <input class="form-control" type="password-log" id="password" />
                        <span></span>
                    </div>
                    <div class="checkbox">
                        <label><input type="checkbox" class="btn btn-default">Pamiętaj mnie</label>
                    </div>
                    <button id="login-button" class="btn btn-success" type="button">Zaloguj się</button>
                </form>
            </div>
        </div>
        <div class="col-sm-1"></div>

JS Code:

    $(function() {
        var checkLogForm=function(){
            alert('anything');
        };

        $('#login-button').on("click", fn)
    });

I don't know why but I can't attach any event to element #login-button. It works even with other elements in form. I've tried also with bind() function but it still doesn't work. Please help!

HTML代码:

<div class="row">
        <div class="col-sm-1 col-md-9"></div>
        <div class="col-sm-10 col-md-3">
            <div class="login-form">
                <form role="form" method="post">
                    <div class="form-group">
                        <label for="nick">Nick:</label>
                        <input class="form-control" type="text" id="nick-log" />
                        <span></span>
                    </div>
                    <div class="form-group">
                        <label for="password">Hasło</label>
                        <input class="form-control" type="password-log" id="password" />
                        <span></span>
                    </div>
                    <div class="checkbox">
                        <label><input type="checkbox" class="btn btn-default">Pamiętaj mnie</label>
                    </div>
                    <button id="login-button" class="btn btn-success" type="button">Zaloguj się</button>
                </form>
            </div>
        </div>
        <div class="col-sm-1"></div>

JS代码:

    $(function() {
        var checkLogForm=function(){
            alert('anything');
        };

        $('#login-button').on("click", fn)
    });

I don't know why but I can't attach any event to element #login-button. It works even with other elements in form. I've tried also with bind() function but it still doesn't work. Please help!

answer1: 回答1:

you can change your html

<button id="login-button" class="btn btn-success">Zaloguj się</button>

instead of

<button id="login-button" class="btn btn-success" type="button">Zaloguj się</button>

and change your js

$(document).on('click', '#login-button', function() {
     alert('anything');
});

I'm not sure but i think it's helpful to you.

你可以改变你的HTML

<button id="login-button" class="btn btn-success">Zaloguj się</button>

而不是

<button id="login-button" class="btn btn-success" type="button">Zaloguj się</button>

改变你的JS

$(document).on('click', '#login-button', function() {
     alert('anything');
});

我不确定,但我认为它对你有帮助。

answer2: 回答2:
   always  Use **ON()** instead of **click()**

enter link description here Check this https://jsfiddle.net/ncsr4tgh/1/ its working.....

   always  Use **ON()** 而不是 **click()**

进入链接说明这里检查这https://jsfiddle.net/ncsr4tgh/1/工作.....

answer3: 回答3:

ChecklogForm or any other function needs to be either outaide of document ready function or both ChecklogForm and binding code need to be inside the same document ready function.

checklogform或任何其他功能的需求是outaide文档准备功能或checklogform和绑定代码需要在同一文档的准备功能。

answer4: 回答4:

in $('#login-button').on("click", fn), fn is name of the callback function which will be called when an click event occurs on the button, which in your case is checkLogForm.

$(“#登录按钮”),(“点击”,FN),FN是回调函数将被调用,在按钮的点击事件发生时的名字,这对你来说是checklogform。

javascript  jquery  events  button