jQuery $("#field") 为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/777062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:46:09  来源:igfitidea点击:

jQuery $("#field") is null?

jquerycss-selectors

提问by sanders

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

$("#ppsub_ppterm_id") 为空

Line 17

17号线

Any ideas?

有任何想法吗?

回答by Mark

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

听起来像 JQuery 没有正确加载。您使用的是哪个源/版本?

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

或者,它可能是命名空间冲突,所以尝试jQuery显式使用而不是$. 如果可行,您可能希望使用noConflict来确保正在使用的其他代码$不会中断。

回答by sanders

chane '$' by jQuery for example:

例如,通过 jQuery 更改 '$':

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

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

It works

有用

回答by Tamas Czinege

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

即使 jQuery 找不到该元素,它也不会为 null - 它会是一个空的 jQuery 对象。

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

你确定 jQuery 已加载?您正在使用的另一个 JavaScript 库是否可能导致冲突?

回答by Chad Grant

you have "ppsub_ppterm_id" as a class, name, id etc...

您有“ppsub_ppterm_id”作为类、名称、ID 等...

You need to pick ONE and select on it. There is no need for ID, NAME, CLASS to all have the same values.

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

You're probably confusing the hell out of jQuery.

您可能对 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]")

Pick a way and go with it, but take out all those redundant attributes.

选择一条路并随它去,但去掉所有那些多余的属性。

回答by Brandon Montgomery

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

确保在加载文档后运行 jQuery 代码:

$(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.

还要确保您的 HTML 页面上没有其他 ID 为“ppsub_ppterm_id”的控件。

Those are the first things I would check.

这些是我要检查的第一件事。