我试图在django管理中实现javascript代码,我有两个字段hard_drives(id is:id_hard_drives和no_of_drives(id is:id_no_of_drives)。因此,我希望只有在no_of_drives具有特殊价值的情况下才出现hard_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>但是,我发现了一个错误:
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上使用key还是什么东西?
发布于 2015-11-16 21:22:07
我已经为你重写了密码。希望这会有帮助!)
$(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();
}
});
});发布于 2015-11-05 13:03:14
下面是您的固定代码
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>https://stackoverflow.com/questions/33545054
复制相似问题