在首页我有一个带有文本类型的输入。在其旁边创建一个按钮。单击按钮时,在 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);
});
最佳答案
尝试使用属性选择器
来抓取元素,
$("#submitNewName").on('click',function(){
alert($('[name=newName]').val());
});
此外,不要使用 live()
,因为它在最新版本的 jquery 中已被弃用
DEMO
关于javascript - js中获取输入类型text的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31724777/