jQuery根据输入值显示输入文本 [英] Jquery Show Input Text Based On Input Value
本文介绍了jQuery根据输入值显示输入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在显示基于输入值的输入文本时,我的jQuery遇到了问题. 这是JS小提琴演示:
I facing problem with my jquery, on showing input text based on input value. Here is the JS fiddle demo :
http://jsfiddle.net/Ltapp/364/
当我尝试输入@hotmail时,将显示输入框.但是,当我想在#hotm输入框中键入一些文本时,它将再次隐藏.
When I try to input @hotmail, the input box will show. But when I want to type some text in the #hotm input box, it will hide again.
JS代码:
$(window).load(function(){
var myString = '@hotmail';
$('#hotm').hide();
$("input").keyup(function () {
var value = $(this).val();
if($(this).val().match(myString)) {
$('#hotm').show();
} else {
$('#hotm').hide();
}
});
});
推荐答案
这是因为选择器$("input")
影响两个输入元素.我已将其更新为$("input:first")
选择器. JsFiddle此处
It's because your selector $("input")
affects both input elements. I have updated it to the $("input:first")
selector instead. JsFiddle here
$("input:first").keyup(function () {
var value = $(this).val();
if(value.match(myString)) {
$('#hotm').show();
} else {
$('#hotm').hide();
}
});
这篇关于jQuery根据输入值显示输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文