js中获取输入类型文本的值

get the value of input type text in js

在首页我有一个输入文本。在它旁边创建了一个按钮。单击按钮时,在 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

的最新版本中已被弃用

DEMO