开发者

Variable not updating in script string, yet it updates

Very confused here.

I have a search box which reads a list of school names from my database. When I select a school, the id (from the db) gets put in a hidden textbox.

I also have a search box which reads a list of courses from my database. However, I made the query so that it only reads the courses from the selected school.

It does that, in theory.

I was planning to pass the school id, which I grab from the hidden box, to the search script which in turn passes it to my database query. However, the variable I put my school id in doesn't seem to be updating.. yet it does. Let me explain.

I come on the page. The school for my test account has id 1. The id number in my hidden box is indeed 1. I search for a school which I know has some courses assigned to it: the id number in the box changes to 3.

I have a JS variable called school_id which I declared outside of my $(document).ready. I assume that means it's global (that's what I got taught even though SO told me once it isn't really the correct way to do this. Still have to look into that). I wrote a function which updates this variable when the school search box loses focus:

    $("#school").blur(fu开发者_JAVA百科nction()    {
        school_id = $("#school_id").val();
    });

A quick javascript:alert(school_id); in my browser bar also shows the updated variable: it is now 3 instead of 1.

Onto the search script part of my page (excerpt of the script):

script:"/profiel/search_richting?json=true&limit=6&id=" + school_id + "&"

As you can see, I pass the school_id variable to the script here. However, what seems to be happening is that it always passes '1', the default variable when the page loads. It simply ignores the updated variable. Does this string get parsed when the page loads? In other words, as soon as the page loads, does it actually say &id=1? That's the only idea I can come up with why it would always pass '1'.

Is there a way to make this variable update in my script string? Or what would be the best way to solve this? I'm probably missing out on something very simple here again, as usual. Thanks a lot.

EDIT

Updated per request. I added a function getTheString as was suggest and I use the value of this function to get the URL. Still doesn't work though, it still seems to be concatenating before I get a chance to update the var. HOWEVER, with this code, my ajax log says id:[object HTMLInputElement], instead of id:1. Not sure what that means.

<script type="text/javascript">
var school_id;
$(document).ready(function()    {

    $("#school").blur(function()    {
        school_id = $("#school_id").val();
    });

    // zoekfunctie
    var scholen = {
        script:"/profiel/search_school?json=true&limit=6&",
        varname:"input",
        json:true,
        shownoresults:false,
        maxresults:6,
        callback: function (obj) { document.getElementById('school_id').value = obj.id; }
    };
    var as_json = new bsn.AutoSuggest('school', scholen);

    var richtingen = {
        script: getTheString(),
        varname:"input",
        json:true,
        shownoresults:true,
        maxresults:6
    };
    var as_json2 = new bsn.AutoSuggest('studierichting', richtingen);

});
function getTheString() {
    return "/profiel/search_richting?json=true&limit=6&id=" + school_id + "&";
}   

   </script>


This is because the URL is static, it is not updated as the ID changes.

You should update the URL as part of the code you wrote to get the ID:

$("#school").blur(function()    {
   school_id = $("#school_id").val();
   // update URL here ...
});


Aren't you concatenating script:"/profiel/search_richting?json=true&limit=6&id=" + school_id + "&" before the event is fired and the var updated?


Okay. So the problem was my third party plug-in instead of the code I wrote. I fixed this by editing the code of the autoSuggest plugin so it now includes my id field in the AJAX request.

    var url = this.oP.script+this.oP.varname+"="+encodeURIComponent(this.sInp)+"&id="+ $("#school_id").val();

Thanks to everyone who tried to help me out!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜