javascript 类型错误:无法读取 null 的属性“值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19398835/
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
TypeError: Cannot read property 'value' of null
提问by Raja Sekar
radio button for answering quiz and my radio button is
用于回答测验的单选按钮,我的单选按钮是
<input id="ans_ans1" name="ans" type="radio" value="ans1">
my next ajax request is
我的下一个 ajax 请求是
<a href="#" onclick="Element.show('loader'); jQuery.ajax({data:'answer='+$('ans').value+'&passed_question=1&'+'&exam_group_id=1&' + '&authenticity_token=' + encodeURIComponent('GGKdjhC39b1Fi5fe52Cq0VcPwpi6laphZblC/5ZOl8o='), dataType:'script', success:function(request){Element.hide('loader')}, type:'post', url:'/answers/next'}); return false;">Next</a>
in my console
在我的控制台中
$('ans').value gives TypeError: Cannot read property 'value' of null
$('ans').value 给出了 TypeError:无法读取 null 的属性 'value'
any one help me to get the value of radio button in ajax request
任何人都可以帮助我获取 ajax 请求中单选按钮的值
回答by T.J. Crowder
It looks to me like you're using a mixof jQuery and either Prototype or MooTools. (See below for why.)
在我看来,您正在混合使用jQuery 和 Prototype 或 MooTools。(原因见下文。)
If so, my first recommendation is stop doing that. Pick one and use it throughout.
如果是这样,我的第一个建议是停止这样做。选择一个并在整个过程中使用它。
But your main problem is that you're looking up an element using an id
("ans"
) that doesn't exist. The id
is ans_ans1
. So either:
但是您的主要问题是您正在使用不存在的id
( "ans"
)查找元素。该id
是ans_ans1
。所以要么:
$("ans_ans1").value // Prototype / MooTools using raw `value` prop
or
或者
$("ans_ans1").getValue() // Prototype
$("ans_ans1").get('value'); // MooTools
or
或者
jQuery("#ans_ans1").val() // jQuery
Why I think you're using a mix of libraries:
为什么我认为您正在使用混合库:
You've said:
$('ans').value gives TypeError: Cannot read property 'value' of null
That makes no sense with jQuery. jQuery's
$
function always returns non-null
. It may return an emptyset, but it will return non-null
. Whereas the$
from either Prototype or MooTools will returnnull
if there's no element with the matchingid
(and there isn't, in your example).When calling jQuery's
ajax
, you're using the symboljQuery
, not$
, which suggests to me you're using jQuery'snoConflict
so that you can have Prototype or MooTools co-exist with it (because those want$
).
你说过:
$('ans').value 给出了 TypeError:无法读取 null 的属性 'value'
这对 jQuery 毫无意义。jQuery 的
$
函数总是返回非null
. 它可能返回一个空集,但它会返回 non-null
。如果没有匹配的元素(在您的示例中没有),则$
来自 Prototype 或 MooTools 将返回。null
id
在调用 jQuery 时
ajax
,您使用的是符号jQuery
,而不是$
,这向我表明您正在使用 jQuerynoConflict
以便您可以让 Prototype 或 MooTools 与其共存(因为那些想要$
)。
回答by Jonathan Lonowski
Since it appears you're using PrototypeJSalong with jQuery -- Element.show()
:
因为看起来您正在使用PrototypeJS和 jQuery -- Element.show()
:
Prototype's $()
expects its argument to be the value of an id
rather a name
. So
Prototype's$()
期望它的参数是一个id
而不是 a的值name
。所以
$('ans').value
should be
应该
$('ans_ans1').value
To match the <input
id="ans_ans1"
...>
in the markup.
匹配标记中的 。<input
id="ans_ans1"
...>
If you'd like to select by name
instead, you can use $$()
with an attribute selector. Though, it'll return a collection.
如果您想选择 by name
,则可以$$()
与属性选择器一起使用。不过,它会返回一个集合。
$$('[name="ans"]')[0].value
回答by gurvinder372
try with
尝试
<a href="#" onclick="Element.show('loader'); jQuery.ajax({data:'answer='+$('#ans_ans1').value+'&passed_question=1&'+'&exam_group_id=1&' + '&authenticity_token=' + encodeURIComponent('GGKdjhC39b1Fi5fe52Cq0VcPwpi6laphZblC/5ZOl8o='), dataType:'script', success:function(request){Element.hide('loader')}, type:'post', url:'/answers/next'}); return false;">Next</a>
回答by Deepu
Change this to
将此更改为
$('ans').value
This
这
$('#ans_ans1').val()
回答by Somnath Kharat
The selector is wrong and the method to retrive value :
选择器错误,取回值的方法:
Modified:
修改的:
<a href="#" onclick="Element.show('loader'); jQuery.ajax({data:'answer='+$('#ans_ans1').val()+'&passed_question=1&'+'&exam_group_id=1&' + '&authenticity_token=' + encodeURIComponent('GGKdjhC39b1Fi5fe52Cq0VcPwpi6laphZblC/5ZOl8o='), dataType:'script', success:function(request){Element.hide('loader')}, type:'post', url:'/answers/next'}); return false;">Next</a>
回答by Vinay Pratap Singh
just use
只是使用
$('#ans_ans1')
or
或者
$("input[id='ans_ans1']")
or
或者
$("input[name='ans']")
to get the value just
Use val()like this $('#ans_ans1').val();
获取值只需
像这样使用val()$('#ans_ans1').val();
I guess you should study some content on jquery selectors
我想你应该研究一些关于jquery 选择器的内容
回答by Arun Chandran C
You should refer this jquery selectors
你应该参考这个jquery 选择器
You can change
你可以改变
$('ans').value
into
进入
$("input[value='ans1']" ).val()
or
或者
$('#ans_ans1').val()