将文本输入中的值传递给 JavaScript 函数
在我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您为用户输入提供
id
属性,这会更容易:现在您可以使用 Javascript 访问该值:
请注意,如果您不控制
Recorder.record() 方法,您需要首先验证用户输入(至少验证他们是否输入了某些内容)。我建议将其移至单独的函数,例如:
然后只需使用该函数:
工作示例: http://jsfiddle.net/nrabinowitz /GFpRy/
This is easier if you give your user input an
id
attribute:Now you can access the value in Javascript with:
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.:Then just use that function:
Working example: http://jsfiddle.net/nrabinowitz/GFpRy/
确保您的表单具有
name="?"
属性才能正常工作,并将yourformname
替换为您输入的表单名称。filename
也指输入的name
属性。Make sure to have a
name="?"
attribute for your form for this to work and replaceyourformname
with what you put as the form's name.filename
refers to the input'sname
attribute as well.