关于Uncaught TypeError: Cannot read property 'toLowerCase' of undefined的问题

转载 :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>
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值