2015-11-05 85 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

沒有在「<」只 –