将文本输入中的值传递给 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7957039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:12:20  来源:igfitidea点击:

Passing value from text input into javascript function

javascripthtmlinputtextfield

提问by Casper Slynge

Inside my html file I call a javascript function that takes two parameters, where the second parameter is the name of a file that should be saved.

在我的 html 文件中,我调用了一个带有两个参数的 javascript 函数,其中第二个参数是应该保存的文件的名称。

<a id="record_button" onclick="Recorder.record('audio', 'test.wav');" href="javascript:void(0);" title="Record"><img src="images/record.png" width="24" height="24" alt="Record"/></a>

I would like to make a dynamic variable that takes the value from a textfield and forward that as the second parameter (instead of test.wav), so the user can determine the name of the file.

我想创建一个动态变量,它从文本字段中获取值并将其作为第二个参数(而不是 test.wav)转发,以便用户可以确定文件的名称。

<label for="filename">Filename</label>
<input name="filename" type="text">

Thanks.

谢谢。

回答by nrabinowitz

This is easier if you give your user input an idattribute:

如果你给你的用户输入一个id属性,这会更容易:

<input name="filename" id="filename" type="text">

Now you can access the value in Javascript with:

现在您可以通过以下方式访问 Javascript 中的值:

document.getElementById('filename').value

Note that, if you aren't controlling the Recorder.record()method, you'll need to validate the user input first (at a minimum, to verify that they've entered something). I'd recommend moving this to a separate function, e.g.:

请注意,如果您不控制该Recorder.record()方法,则需要首先验证用户输入(至少,以验证他们是否输入了某些内容)。我建议将其移至单独的功能,例如:

function recordToFilename() {
    var input = document.getElementById('filename'),
        fileName = input.value;
    if (fileName) {
        Recorder.record('audio', fileName);
    } else {
        alert('Please enter a filename!');
        input.focus();
    }
}

Then just use that function:

然后只需使用该功能:

<a id="record_button" onclick="recordToFilename();" href="javascript:void(0);" title="Record"><img src="images/record.png" width="24" height="24" alt="Record"/></a>

Working example: http://jsfiddle.net/nrabinowitz/GFpRy/

工作示例:http: //jsfiddle.net/nrabinowitz/GFpRy/

回答by casraf

Recorder.record('audio', document.yourformname.filename.value)

Make sure to have a name="?"attribute for your form for this to work and replace yourformnamewith what you put as the form's name. filenamerefers to the input's nameattribute as well.

确保name="?"您的表单有一个属性才能工作并替换yourformname为您作为表单名称的内容。filename也指输入的name属性。

回答by Vijay Makwana

<html>
<head>
  <script>
     function addData (fn, ln, em) {

       console.log(fn);
       console.log(ln);
       console.log(em);

     }
 </script>
</head>
<body>
    <input Type="text" name="FirstName" id="fn">
    <input Type="text" name="LastName" id="ln">
    <input Type="email" name="Email" id="em">
    <button onClick="addData(fn.value,ln.value,em.value)">click</button> 
</body>
</html>