转载 :https://blog.csdn.net/MrZZhou/article/details/59106805
今天我在处理表单时根据触发事件动态获取input中的value值,我是直接在html代码中设置响应函数,函数中用jquery的$(this).val()获取对象的value,结果控制台报出如标题所示的错误,这我就纳了闷了,怎么会这样,甚是纠结,上网一查排在前的相关问题都是stackoverflow的,找了几个看了下,好像不是我的这种情况,然后就各种尝试….
最后突然想到会不会连input这个节点对象没获取到啊,一试还真的是,于是我就换了("input[name=′example′]"),就可以了,为了保险起见,我另外进行了样式修改,结果确实是jquery的("input[name=′example′]"),就可以了,为了保险起见,我另外进行了样式修改,结果确实是jquery的(this)在input这出了点状况。
html:
<script type="text/javascript">
function check(){
console.log($(this));
$(this).attr("style","border:1px red solid");
console.log($(this).val());
// console.log($("input[name='example']"));
//$("input[name='example']").attr("style","border:1px red solid");
// console.log($("input[name='example']").val());
}
</script>
</head>
<body>
<input type="text" name="example" value="0" onchange="check()">
</body>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
wrong:
right:
<script>
(function(){
function setArticleH(btnReadmore,posi){
var winH = $(window).height();
var articleBox = $("div.article_content");
var artH = articleBox.height();
if(artH > winH*posi){
articleBox.css({
'height':winH*posi+'px',
'overflow':'hidden'
})
btnReadmore.click(function(){
articleBox.removeAttr("style");
$(this).parent().remove();
})
}else{
btnReadmore.parent().remove();
}
}
var btnReadmore = $("#btn-readmore");
if(btnReadmore.length>0){
if(currentUserName){
setArticleH(btnReadmore,3);
}else{
setArticleH(btnReadmore,1.2);
}
}
})()
</script>
</article>