简体   繁体   中英

.click() function doesn't work javascript / jquery

UPDATE:

Somehow the just work fine after I tried it..

UPDATE

I'm using .click() function on my javascript, but it doesn't work.

Here is my JS code:

src="jquery.js";
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
var institutionID, acadCareerID;
$(document).ready(function(){


    getInstitution();

    institutionID = $( "select#institution option:checked" ).val();
    if(institutionID == null){
        institutionID = 1;
    }

    getAcadCareerByInstitution(institutionID);

    acadCareerID = $( "select#acadCareer option:checked" ).val();
    if(acadCareerID == null){
        acadCareerID = 1;
    }

    getPeriodByAcadCareerAndInstitution(acadCareerID);
    getDepartmentByAcadCareer(acadCareerID);



    $("select#institution").change(function(){
        institutionID = $( "select#institution option:checked" ).val();
        getAcadCareerByInstitution(institutionID);
    })
    $("select#acadCareer").change(function(){
        acadCareerID = $( "select#acadCareer option:checked" ).val();
        getPeriodByAcadCareerAndInstitution(acadCareerID);
        getDepartmentByAcadCareer(acadCareerID);
    })


    $("div#search").click(function(){
        alert("The paragraph was clicked.");
        console.log("abc");
    });

});

all functions like getInstituion() , getAcadCareerByInstitution(institutionID) etc are ajax call.

Here is my HTML code:

<form action="doInsertSchedule" method="POST" enctype="multipart/form-data">
                {{csrf_field()}}
                <div class="form-group">
                    <label for="institution">Institution</label>
                    <select name="institution" class="form-control" id="institution">
                    </select>
                </div>
                <div class="form-group">
                    <label for="acadCareer">Academic Career</label>
                    <select name="acadCareer" class="form-control" id="acadCareer">
                    </select>
                </div>
                <div class="form-group">
                    <label for="period">Period</label>
                    <select name="period" class="form-control" id="period">
                    </select>
                </div>
                <div class="form-group">
                    <label for="department">Department</label>
                    <select name="department" class="form-control" id="department">
                    </select>
                </div>
                <div class="form-group">
                    <label for="date">Deadline Date</label>
                    <input type="date" class="form-control" id="deadline" placeholder="Deadline Date" name="deadline">
                </div>

                <button type="submit" class="btn btn-default">Submit</button>
                <div id="search" class="btn btn-default">Search</div>
            </form>

I already put jquery for my Master Layout (Laravel 5)

<head>
        <link rel="stylesheet" type="text/css" href="{{url()}}/css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="{{url()}}/css/style.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">        
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="{{url()}}/js/tools.js"></script>
        <script src="{{url()}}/js/bootstrap.min.js"></script>
        <title>BINUS - @yield('title')</title>
        <meta name="csrf-token" content="{{ csrf_token() }}" />
</head>

I want the Search Button / Div to do something when I click. I can't see what's wrong here.

I already tried <div> to <button> but it just will work like submit. I also already tried to put event.preventDefault() at my JS code, but it didn't work too.

I don't have the reputation to comment, but have you added a jquery library before your script?

Secondly, if you did and the items on the page are loaded after the document has loaded you will need to change your code up, for instance: Give your form an id

<form action="doInsertSchedule" method="POST" enctype="multipart/form-data" id="postForm">

Then in your jquery script

<script>
$('#postForm').on('click', '#search', function(){
    alert("The paragraph was clicked.");
    console.log("abc");
});
</script>

The above will allow the javascript to run on items that are bought in after document load, however it is important you set it on a parent element that is avaiable at document load

Maybe you can try with event delegation...

$(document).ready(function(){

    $("body").on("click", "#search", function(){
        alert("The paragraph was clicked.");
        console.log("abc");
    });

});

You code works OK. You need to write the jquery library in the final, just before of </body>

    ...
    <script type="text/javascript" src=".../jquery.js"></script>
</body>

Example it works ok!: Example Jsfiddle

<button type="submit" class="btn btn-default">Submit</button>
<div id="search" class="btn btn-default">Search</div>

This looks like it will render on the page as two buttons. Your ID here is attached to the div, not to the button, they should both appear to look like buttons providing bootstrap is loaded, but only the div with "search" as the content will do anything.

This is the only thing that appears odd, the only other problem is you are not loading your jquery as stated elsewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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