所以我做了一个表格,我试图在处理之前用javascript验证它。每个其他领域都可以正常工作。这是表格的外观。

<form name="game_sub" action="add_game.php" onsubmit="return gameVal()" method="post">
        <table>

            <tr><td><input type="text" name="name" class="game_name" placeholder="NAME" /></td></tr>
            <tr><td>
            <select name="keyword" >
                <option value="action">Action</option>
                <option value="platform">Platform</option>
                <option value="sport">Sport</option>
                <option value="shooter">Shooter</option>
            </select>
            </td></tr>
            <tr><td><label>Game: </label><input type="file" name="game_file" id="g_file"></td></tr>
            <tr><td><label>Thumbnail: </label><input type="file" name="game_thumb" id="t_file"></td></tr>
            <tr><td><textarea name="desc" placeholder="DESCRIPTION"></textarea></td></tr>
        </table>
        <a href="index.php"><button type="button" id="add_btn_lft" class="add_btn">CANCEL</button></a>
        <input type="submit" value="SUBMIT" id="add_btn_rgt" class="add_btn"/>


然后,我添加了javascript。 javascript中有一小行jQuery,通过它我已经将google jquery源脚本添加到页面顶部。我完美地工作了大约1个小时,我离开了,然后回来,它坏了。我通过将一些内容转为注释来检查问题所在,以查看是否缺少它可以使其再次正常工作。我做了,当我删除变量时:

var TSize = document.forms.game_sub.game_thumb.form[0].size;


整个剧本复活了。我正在使用jQuery的.size来获取文件的大小,并且工作了一段时间,但我不知道发生了什么...它只是随机地决定不起作用。 javascript在其中执行jQuery是否存在问题?我以前做过,没问题,但这很奇怪。

这是我的.js文件的外观:

function gameVal() {

  var GName = document.forms.game_sub.name.value;
  var GCat = document.forms.game_sub.keyword.value;
  var GFile = document.forms.game_sub.game_file.value;
  var GThumb = document.forms.game_sub.game_thumb.value;
  var Tarea = document.forms.game_sub.desc.value;
  var BErr = document.getElementById("add_game_err");
  var GFile2 = GFile.split(".");
  var GThumb2 = GThumb.split(".");
  var imgExt = ["jpg", "jiff", "jpeg", "gif", "tiff"];
  var gameExt = ["swf", "unity3d"];

  if (window.File && window.FileReader && window.FileList && window.Blob) {

  if (GFile == "" || GThumb == "" || GName == "" || GName == null || GCat == "Category" || Tarea == "") {
    BErr.style.display = "block";
    BErr.innerHTML = "Please fill in all the fields.";
    return false;
  }
  else if (GFile2.length > 2) {
    BErr.style.display = "block";
    BErr.innerHTML = "Please make sure the game file name has no periods(.) within it.";
    return false;
  }
  else if (GThumb2.length > 2) {
    BErr.style.display = "block";
    BErr.innerHTML = "Please make sure the thumbnail file name has no periods(.) within it.";
    return false;
  }
  else if (imgExt.indexOf(GThumb2[1]) == -1) {
    BErr.style.display = "block";
    BErr.innerHTML = "Thumbnail file format is invalid. (Only JPG, JIFF, JPEG, GIF, TIFF are allowed.)";
    return false;

  }
  else if (gameExt.indexOf(GFile2[1]) == -1) {
        BErr.style.display = "block";
        BErr.innerHTML = "Game file format is invalid.";
        return false;
  }
  else {
  var TSize = document.forms.game_sub.game_thumb.form[0].size;
  if (TSize > 1000000) {
    BErr.style.display = "block";
    BErr.innerHTML = "Thumbnail is too big. (Max size: 1MB)";
    return false;
  }
  else {
    BErr.style.display = "block";
    BErr.innerHTML = "Error";
    return false;
  }
  }
}
else {
    BErr.style.display = "block";
    BErr.innerHTML = "Your browser is outdated and cannot support file upload. Please update to latest version.";
    return false;

}
}


我在本地服务器和jsbin.com上对其进行了测试,并且可以正常工作。但奇怪的是,随着时间的流逝,它以某种方式停止工作。这已经发生了两次。昨晚我在处理它,然后在今天早晨打开它,发生了同样的事情。我修好了它。然后今天下午,又看了一遍,同样的东西...

最佳答案

该代码无法完成您所想的任何事情,并且该代码中没有jQuery。

form元素的input属性返回其所在形式,因此document.forms.game_sub.game_thumb.form返回与document.forms.game_sub相同的值。

在表单上使用[0]会获得表单中的第一个字段,即名称输入字段,而在.size上使用size会从名称输入字段读取属性size

input属性用于以字符指定files的宽度。

我认为您正在寻找属性:

var TSize = document.forms.game_sub.game_thumb.files[0].size;

10-05 20:48
查看更多