在文本输入时显示div [英] Show div on text entry

查看:92
本文介绍了在文本输入时显示div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,我想根据在文本字段中输入的文本显示一个div.我正在使用的脚本在最新版本的chrome上可以正常使用,但在IE或我的任何浏览器上均无法使用.他们到底在想什么脚本错误?

I have a form and I'd like to show a div based on the entered text in a text field. The script I'm using works just fine on my latest version of chrome but it doesn't work on IE or any of my members browswers. What they heck is wrong with the script?

    <script>
$('document').ready(function(){
$('#gname').change(function() {
($(this).val() == "NBA 2k10") ? 
$('#number').slideDown('fast') : $('#number').hide();
})
});
</script>

推荐答案

$("document")在其他浏览器中不是有效的选择器,您需要$(document),就像这样:

$("document") isn't a valid selector in other browsers, you need $(document), like this overall:

$(document).ready(function(){
  $('#gname').keyup(function() { //probably want keyup instead of change here
    if ($(this).val() == "NBA 2k10") 
      $('#number').slideDown('fast')
    else 
      $('#number').hide();
  });
});

您可以在此处进行测试.

$(document).ready(function(){也可以只是$(function(){. $("document")不起作用,因为它正在寻找<document>元素,该元素不存在.

$(document).ready(function(){ can also just be $(function(){. $("document") doesn't work because it's looking for <document> element, which doesn't exist.

if/else的更改只是为了使它总体上更整洁,这在某种程度上来说是一种偏爱,但通常来说,您的条件语句不应有副作用,这不是他们的意图.您可以以这种方式使用它们,但是最好是明确的,至少在我看来.

The if/else change is just to make it a bit cleaner overall, it's somewhat preference, but generally speaking your conditional statements shouldn't have side effects, that's not their intention. You can use them that way, but it's better to be explicit, at least in my opinion.

这篇关于在文本输入时显示div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆