使用 jQuery 从表中获取输入值
现在,我的 HTML 代码如下所示:
<div id="register">
<table class="tab2">
<tr>
<td>Email:</td>
<td><input type="text" id="email" /></td>
</tr>
<tr>
<td>Confirm Email:</td>
<td><input type="text" id="confirmemail" /></td>
</tr>
</table>
</div>
为了通过 jQuery 从输入中获取值,我必须这样做:
$("#email").live('focusout', function() {
email = $(this).val();
});
$("#confirmemail").live('focusout', function() {
confirmemail = $(this).val();
});
我知道有更好的解决方案来执行此操作(因为我的解决方案有效,但是很混乱,因为有两个以上的输入),但它们对我不起作用。我想使用类似的东西
$("#email").val();
,只是根据需要检索值,而不是将其存储到变量中。问题是,每当我运行该代码时,它总是返回一个空字符串。原因是什么?
编辑:
我忘了提及这些值是在单击事件运行后使用的,如下所示:
$("#dialog_next").live('click', function() {
当我之后运行 $("#email").val() 代码时,它返回一个空字符串。
Right now, my HTML code looks like so:
<div id="register">
<table class="tab2">
<tr>
<td>Email:</td>
<td><input type="text" id="email" /></td>
</tr>
<tr>
<td>Confirm Email:</td>
<td><input type="text" id="confirmemail" /></td>
</tr>
</table>
</div>
And to get the values from the inputs via jQuery, I have to do this:
$("#email").live('focusout', function() {
email = $(this).val();
});
$("#confirmemail").live('focusout', function() {
confirmemail = $(this).val();
});
I know there are better solutions to perform this (as my solution works, but is messy since there's way more than two inputs), but they won't work for me. I'd like to use something like
$("#email").val();
and just retrieve the value as it's needed, rather than storing it to a variable. The issue is that whenever I run that code, it always returns an empty string. What would be the reason for that?
EDIT:
I forgot to mention that the values are being used after a click event is ran, like so:
$("#dialog_next").live('click', function() {
When I run the $("#email").val() code after that, it returns an empty string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:这个小提琴工作正常: http://jsfiddle.net/yt8pK/
我希望这个问题有一些问题代码实际运行时执行的操作。由于我看不到问题代码,我只能猜测。例如,如果您有这样的情况:
它将失败,因为变量电子邮件是在用户输入之前设置的。
像这样的代码可以工作:
你失败的代码是什么样子的?
Update: This fiddle works fine: http://jsfiddle.net/yt8pK/
I expect this issue has something to do with when the code is actually run. Since I can't see the problem code I can only guess. For example if you had like this:
It would fail as the variable email is set before user input.
Code like this would work:
What did your code which failed look like?