Javascript文件的特定部分未运行

问题描述 投票:0回答:1

我最近将我的Web文件从CodePen传输到了Web主机。虽然代码现在在CodePen中运行良好,但是我的函数在选择其他关联的文本字段时从文本输入中删除值的功能不起作用。 JS文件的其余部分运行良好,包括使用JQuery的部分。这是有问题的代码:

//Clear out input fields when not selected
$("#sg").focusin(function() {
  $("#density").val("");
});
$("#density").focusin(function(){
  $("#sg").val("");
});
$("#pounds").focusin(function() {
  $("#grams").val("");
  $("#percentage").val("");
});
$("#grams").focusin(function() {
  $("#percentage").val("");
  $("#pounds").val("");
});
$("#percentage").focusin(function(){
  $("#pounds").val(""); 
  $("#grams").val("");
});
javascript jquery codepen
1个回答
0
投票

最有可能在加载DOM之前执行代码。尝试将代码包装在$(function(){})

中。
//Clear out input fields when not selected
$(function(){

$("#sg").focusin(function() {
  $("#density").val("");
});
$("#density").focusin(function(){
  $("#sg").val("");
});
$("#pounds").focusin(function() {
  $("#grams").val("");
  $("#percentage").val("");
});
$("#grams").focusin(function() {
  $("#percentage").val("");
  $("#pounds").val("");
});
$("#percentage").focusin(function(){
  $("#pounds").val(""); 
  $("#grams").val("");
});

});
© www.soinside.com 2019 - 2024. All rights reserved.