开发者

JQuery function not working - sometimes [closed]

开发者 https://www.devze.com 2023-04-11 20:37 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

I have 2 identical code blocks, when it is loaded at first from the index in controller it works, but when I submit a form and load this code, my autocomplete isn't working for a field.

<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>public/blue.css" />
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css" />    
<script type="text/javascript" src="<?php echo base_url(); ?>public/jquery-1.6.1.js"></script>    
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js"></script>
<script type="text/javascript">

        $(document).ready(function() {
            alert('workin');


            //$(".publish-bottom").hide();
            $(".publish-right").hide();



            //title field
            $.ajax({

                type: "GET",
                cache: false,
                dataType: 'json',
                url: "publishlinks/search_movies",
                success:
                    function(response) {

                        alert('response received');
                        alert(response);

                        $("#title").autocomplete({
                            source: response,
                            minLength: 2,
                            select: function (event, ui) {

                                var selectedObj = ui.item;
                                var imgFilename = ui.item;                              
                                imgFilename = imgFilename.value;
                                imgFilename = imgFilename.replace(/ /g,"_");

                                $(".selected-right").empty();
                                $(".selected-left").empty();
                                $(".publish-right").show();

                                //render selected media
                                $(".selected-left").append('<a href="<?php echo "#"; ?>"> <img class="movie-img" src="<?php echo base_url(); ?>img_test/' + imgFilename + '"></a>');
                                $(".selected-right").append('<h4>' + selectedObj.title + '</h4>');
                              开发者_运维问答  $(".selected-right").append(selectedObj.plot);

                                //update publication id  
                                $("#movieId").attr("value", selectedObj.movieid);
                                $("#selected-item").attr("value", "selected");

                                $(".new-movie").show();

                                //get selected item's publications
                                getPublications(selectedObj);

                            }

                        }).data( "autocomplete" )._renderItem = function( ul, item ) {

                            var imgFilename = item.label;
                            imgFilename = imgFilename.replace(/ /g,"_");

                            return $( "<li></li>" )
                                .data( "item.autocomplete", item )
                                .append( '<a>' + '<img src="http://localhost/portal/img_test/' + imgFilename + '" width="40" height="63" />' + item.title + '</a>' )
                                .appendTo(ul);
                        }

                    }

             });

        });

</script>

function search_movies() {

    $movies = $this->movie_model->get_movies();     
    echo json_encode($movies);

}

//works fine    
function index() {

            $this->load->model('user_model');
            $data['user'] = $this->user_model->get_user($this->session->userdata('userid'));

            $data['loggedIn'] = $this->is_logged_in() ? true : false;
            $this->load->view('header_view', $data);
            $this->load->view('publish_links_view');

}

Code for form submit:

            //Not working for autocomplete
            $error['linksError'] = 'You must select an item to publish links for.';         

            $data['user'] = $this->user_model->get_user($this->session->userdata('userid'));
            $data['loggedIn'] = $this->is_logged_in() ? true : false;
            $this->load->view('header_view', $data);
            $this->load->view('publish_links_view', $error);


I agree with user555116, we don't have enough information. But I do see one problem with your code. This may be the issue, may not. You have your functions defined in this block, you say:

$(function() {

});

Well that, is the short form of this:

$(document).ready(function() {


});

For both blocks, all jquery does is save the code in these blocks. When jquery recieves the DOM loaded event, it runs each function, in the order that you sent it to jquery. Also, each of these functions do not implicitly have access to the other. Unless you create a reference to the function defined inside the doc ready blocks in some way, they will disappear after the block executes. However, in this case, the easier (and more correct) solution is to simply remove function definitions that from the $(function() { }).

0

精彩评论

暂无评论...
验证码 换一张
取 消