在首页中,我有一个文本输入。在旁边创建一个按钮。当单击按钮时,在js中,我想在输入中使用文本。但是我得到这个错误:
Cannot read property 'value' of null
首页:
New Name: <input type="text" name="newName" value= "blahblah" > </input>
<button id="submitNewName">OK!</button>
Js:
$("#submitNewName").live('click',function(){
alert(document.getElementById('newName').value);
});
尝试用它attribute selector
来抓取元素,
$("#submitNewName").on('click',function(){
alert($('[name=newName]').val());
});
以及,不要使用,live()
因为它在最新版本的jquery中已弃用
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句