简体   繁体   English

从HTML表单字段中提取Javascript函数值

[英]Javascript Function values pulled from HTML form fields

I am trying to create a HTML system, it creates a .txt form through phonegap. 我正在尝试创建一个HTML系统,它通过phonegap创建一个.txt表单。

my HTML elements are as such 我的HTML元素就是这样

SCRIPT 脚本

function Savenote() {
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("DCC.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log(" ' '");
        writer.truncate(11);  
        writer.onwriteend = function(evt) {
            console.log(" ' '");
            writer.seek(0);
            writer.write("Henry Aspden");
            writer.onwriteend = function(evt){
                console.log(" ' '");
            }
        };
    };
    writer.write("some sample text");
}

function fail(error) {
    console.log(error.code);
}
}
</script>

MY BODY/FORM 我的身体/表格

  <form>
<input type="text" style="width:100%" name="filename" placeholder="Enter File Name">
<textarea rows="10" style="width:100%" name="notes" placeholder="Enter Your Text Notes Here"></textarea>
</form>
<a href="#" onClick="Savenote()"><h1>SAVE</h1></a>

WHATS MY QUESTION THEN? 那我的问题是什么? I need the values in my function to be taked from the form fields... and this is how if possible 我需要从表单字段中获取函数中的值...这是可能的方式

Where it says 它说的地方

fileSystem.root.getFile("DCC.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

the "DCC.txt" section must be replaced by the field "filename" (ie "DCC.txt" is just for demo purposes). “ DCC.txt”部分必须替换为“文件名”字段(即“ DCC.txt”仅用于演示目的)。 Also, the extension .txt is a constant, so it should be 'filename'.txt if that make any sense? 另外,扩展名.txt是一个常量,因此如果有意义的话,它应该是'filename'.txt。

Where it says 它说的地方

writer.write("Henry Aspden");

the "Henry Aspden" section must be replaced by the field "notes" (ie "Henry Aspden" is just for demo purposes) “ Henry Aspden”部分必须替换为“ notes”字段(即“ Henry Aspden”仅用于演示目的)

EDIT NUMBER 1 Changed to 编辑编号1更改为

    function gotFS(fileSystem) {
    fileSystem.root.getFile("var filename = document.getElementById("filename");", {create: true, exclusive: false}, gotFileEntry, fail);
}

with

<input id="filename" type="text" style="width:100%" name="filename" placeholder="Enter File Name">

results in a syntax error here i think... how can I place this new variable within this existing function?? 我在这里导致语法错误...我如何将这个新变量放在现有函数中?

Thanks 谢谢

Give the input elements an id: 给输入元素一个ID:

<input id="filename" 
    type="text" style="width:100%" name="filename" placeholder="Enter File Name">

Then grab the element and save it as a JavaScript variable 然后抓取元素并将其另存为JavaScript变量

var filename = document.getElementById("filename").value;

Put this somewhere before your function call, then pass the variable to your function: 将其放在函数调用之前,然后将变量传递给函数:

function gotFS(fileSystem) {
    var filename = document.getElementById("filename").value; //gets file name

    fileSystem.root.getFile( //call function 
        filename, {create: true, exclusive: false}, gotFileEntry, fail
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM