17

I have a selectbox with month periods in it.

Here is my code:

$(function(){ 
                        $("#ppsub_ppterm_id").change(function(){ 
                                        var term = this.options[this.selectedIndex].text; 
                                        if(term == "Eenmalig"){ 
                                                $(".idealtd").show(); 
                                        }else{ 
                                                $(".idealtd").hide(); 
                                                //$("#ppsub_amount option:selected").val('anders'); 
                                        } 
                        }); 
        }); 
<select name="ppsub_ppterm_id" class="ppsub_ppterm_id" 
id="ppsub_ppterm_id" style="width: 100px; font-size: 11px;"> 
                                                <option value="M">Maand</option> 
                                                <option value="K">Kwartaal</option> 
                                                <option value="H">Halfjaar</option> 
                                                <option value="J">Jaar</option> 
                                                <option selected value="E">Eenmalig</option> 
                                        </select> 

But when i load my page i staight away get an error:

$("#ppsub_ppterm_id") is null

Line 17

Any ideas?

4

5 回答 5

41

Sounds like JQuery isn't loading properly. Which source/version are you using?

Alternatively, it could be namespace collision, so try using jQuery explicitly instead of $. If that works, you may like to use noConflict to ensure the other code that's using $ doesn't break.

于 2009-04-22T12:55:11.913 回答
6

例如 jQuery 的 chane '$':

$("#myId") -> jQuery("#myId")

有用

于 2009-10-12T23:08:34.490 回答
3

Even if jQuery couldn't find the element, it wouldn't be null - it would be an empty jQuery object.

Are you sure jQuery is loaded? Is it possible that another JavaScript library you're using is causing conflicts?

于 2009-04-22T12:54:17.547 回答
2

您将“ppsub_ppterm_id”作为类、名称、id 等...

您需要选择一个并选择它。ID、NAME、CLASS 不需要都具有相同的值。

你可能把 jQuery 搞糊涂了。

<a id="ppsub_ppterm_id"> = $("#ppsub_ppterm_id")

<a class="ppsub_ppterm_id"> = $(".ppsub_ppterm_id")

<a name="ppsub_ppterm_id">  = $("*[name=ppsub_ppterm_id]")

选择一种方法并坚持下去,但要去掉所有那些多余的属性。

于 2009-04-22T13:16:59.427 回答
0

Make sure you're running your jQuery code after the document is loaded:

$(document).ready(function() { /* put your stuff here */ });

Also make sure you have no other controls with the id "ppsub_ppterm_id" on your HTML page.

Those are the first things I would check.

于 2009-04-22T12:53:18.920 回答