Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript向用户输入字段添加文件扩展名_Javascript_Html - Fatal编程技术网

如何使用javascript向用户输入字段添加文件扩展名

如何使用javascript向用户输入字段添加文件扩展名,javascript,html,Javascript,Html,我正试图通过ajax请求从服务器检索txt文档。txt文档的名称取决于html文档上输入的文本。基本上,我想在onclick事件之后将.txt追加到输入字段的末尾 // JavaScript Document function getData(){ var xmlhttp; var user=document.getElementById("nameDetails").value; var userText = user + ".txt"; //**not the sol

我正试图通过ajax请求从服务器检索txt文档。txt文档的名称取决于html文档上输入的文本。基本上,我想在onclick事件之后将.txt追加到输入字段的末尾

// JavaScript Document
function getData(){
    var xmlhttp;
    var user=document.getElementById("nameDetails").value;
    var userText = user + ".txt"; //**not the solution
       if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
       }
       else
       {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
       xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
      document.getElementById("userSubmit").innerHTML=xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET","userText",true);
    xmlhttp.send();
}

如果要将
.txt
附加到输入字段本身,可以尝试以下操作:

document.getElementById("nameDetails").value = document.getElementById("nameDetails").value + ".txt";
或简称:

document.getElementById("nameDetails").value += ".txt";

你的解决方案有什么问题?在我看来,这应该行得通。这里到底是什么问题?为什么
var userText=user+“.txt”不工作?如果您只是试图从
nameDetails
获取输入并向其中添加.txt,那么应该可以。我是不是遗漏了什么?
nameDetails
不是用户输入文件名的文本框吗?似乎您想要
xmlhttp.open(“GET”,userText,true)-使用变量而不是传递字符串文字