2015-11-05 139 views
0

我是想实现我的Django管理一个javascript代码,我有两个字段hard_drives(id为:id_hard_drives)和no_of_drives(id为:id_no_of_drives)。所以,我想no_of_drives出现只有hard_drives具有特殊的价值,像例如:意外标记<在JS

<script type="text/javascript"> 
    $("#id_hard_drives").change(function() { 
     if($("#id_hard_drives").val()=="125"){ 
      document.getElementById("#id_no_of_drives").type=text 
     } else { 
      document.getElementById("#id_no_of_drives").type=hidden 
     } 
    }); 
</script> 

但是,我得到一个错误:

Unexpected token < 

更新

根据GSWV,我已更新的代码,但它仍然用来显示相同​​的错误,所以我删除<script>标签。新的代码看起来是这样的:

(function($) { 
    $(document).ready(function() { 
     var hardDriveSelector = $("#id_hard_drives"); 
     hardDriveSelector.on("change", function(){ 
      if (hardDriveSelector.val() == "H") { 
       document.getElementById("id_no_of_drives").type = text; 
      } else { 
      document.getElementById("id_no_of_drives").type = hidden; 
      } 
     }); 
    }); 
})(django.jQuery); 

但代码不被飞实施,脚本这么想的做任何事情,我需要使用钥匙向上或某事上id_hard_drives

+0

使用语法hilighting和Linter获取编辑器。请参阅:http://stackoverflow.com/questions/6803305/should-i-use-jslint-or-jshint-javascript-validation $(“。#id_hard_drives”)。change(function(){ \t if($ ( “#id_hard_drives”)VAL()== 125){ \t \t的document.getElementById( “id_no_of_drives”)类型=文本; \t}。 \t否则{ \t \t文档。getElementById(“id_no_of_drives”)。type = hidden; \t} }); – digitaldonkey

+0

欢迎来到Stack Overflow!我编辑了你的问题,删除了谢谢和其他句子,因为没有必要。 – jezrael

回答

1

我已经重新编写的代码之前您。希望这可以帮助! :)

$(document).ready(function() { 
    $('#id_no_of_drives').hide(); 
    $("#id_hard_drives").change(function() { 
     var driveID = $(this).val(); 
     if (driveID == '125') { 
      $('#id_no_of_drives').show(); 
     } else { 
      $('#id_no_of_drives').hide(); 
     } 
    }); 
}); 
+0

它的工作第一线,非常感谢:) –

+0

欢迎您:)工作的人,其示值误差 –

-1

这显然是一个语法问题,我已经重新编写下面的代码的方式,我相信会的工作:

<script type="text/javascript"> 
    var hardDriveSelector = $("#id_hard_drives"); 
    hardDriveSelector.on("change", function(){ 
     if (hardDriveSelector.val() == "125") { 
      document.getElementById("id_no_of_drives").type = text; 
     } else { 
      document.getElementById("id_no_of_drives").type = hidden; 
     } 
    }); 
</script> 

基本上我做了以下修改:

  1. 我我创建了一个包含id_hard_drives的JQuery选择器的变量(注意你不小心使用了TWO选择器 - .#id_hard_drives而不是一个 - #id_hard_drives)。
  2. 而不是使用.change() JQuery函数,我用.on()函数与“更改”参数,更多信息查看this链接。
  3. 请注意,在getElementById函数中,您使用了一个JQuery选择器 - 它应该被删除(不包括#)。
+0

不起作用,它在第一行“<”显示错误 –

+0

@DrukNarng你在哪里运行? 你能告诉我们你的整个页面吗? 也许你有一个挥之不去的PHP闭合标签或一个挥之不去的HTML标记,只是没有足够的信息。 – spongeworthy

+0

并没有反对票的东西,只是因为它不为你工作,这只是不好的做法,用下来,票只对错误/无益/不相干的答案... – spongeworthy

-1

下面是你的固定码

1)$("#id_hard_drives") - 前#id_hard_drives

2)document.getElementById('id_no_of_drives')没有点 - 无#id_no_of_drives

<script type="text/javascript"> 
    $("#id_hard_drives").change(function() { 
     if ($("#id_hard_drives").val() === '125') { 
      document.getElementById('id_no_of_drives').type = text; 
     } else { 
      document.getElementById('id_no_of_drives').type = hidden; 
     } 
    }); 
</script> 
+0

===“125”意味着值必须是一个字符串 == 125是坏的风格,但会让你的生活更轻松。 – digitaldonkey

+0

没有在“<”只 –