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

发布于 2024-12-13 02:52:47 字数 494 浏览 3 评论 0原文

在我的 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>

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

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

谢谢。

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.

<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.

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别低头,皇冠会掉 2024-12-20 02:52:47

如果您为用户输入提供 id 属性,这会更容易:

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

现在您可以使用 Javascript 访问该值:

document.getElementById('filename').value

请注意,如果您不控制 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();
    }
}

然后只需使用该函数:

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

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

This is easier if you give your user input an id attribute:

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

Now you can access the value in Javascript with:

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.:

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/

停滞 2024-12-20 02:52:47
Recorder.record('audio', document.yourformname.filename.value)

确保您的表单具有 name="?" 属性才能正常工作,并将 yourformname 替换为您输入的表单名称。 filename 也指输入的 name 属性。

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

Make sure to have a name="?" attribute for your form for this to work and replace yourformname with what you put as the form's name. filename refers to the input's name attribute as well.

昇り龍 2024-12-20 02:52:47
<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>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文