由于某种原因,我的输入值不会显示在控制台.
这是我的代码,
HTML:
<tr class="area"><td class="icon"></td>
<td class="title">
<div>
User: <input id="inputUser" type="text" style="margin-top: 2px;margin-left:2px"></input>
Ammount: <input id="inputAmmount" type="text" style="margin-top:2px;margin-left:2px; padding-right: 0px"></input>
</div>
<td><a class="btn" style="margin-top:3px">Submit</a></td>
</td>
</tr>
字符串
JQUERY
$('.btn').click(function(){
console.log("click detected");
var userID = $('#inputUser').val();
var ammount = $('#inputAmmount').val();
console.log(userID);
});
型
控制台成功输出“检测到点击”,但对于用户ID,它不显示任何内容。
2条答案
按热度按时间sc4hvdpw1#
您的代码适用于我:http://jsfiddle.net/1reLtunc/
我唯一能推荐的是,在为
$('.btn').click
创建事件侦听器时,请确保HTML已经呈现,并且没有通过apache加载。如果它是通过aplog加载的,您将需要在aplog调用完成后重新绑定事件侦听器。
此外,请确保将
$('.btn').click
事件侦听器 Package 在$(document).ready(function() { /* your code here */ })
或简写等价物中:$(function() { /* your code here */ })
7vux5j2d2#
我犯过几次类似的错误。当你使用document.getElementByID时,脚本已经识别出你正在使用一个ID,所以你不需要包含hashtag。document.getElementByID('inputUser').value;将工作。