将单选按钮的值设置为文本输入
Setting value of a radio button into a text input
我必须做5个单选按钮,每个按钮的值为10,15,20,25,30
(所以第一个收音机是10,第二个是15,依此类推)
如果我单击单选按钮,该值应显示在文本输入中。我真的不知道该怎么做。有一件事,我真的需要继续使用java和html...嘿。另外,我的老师要求我使用对象"this"(我从未使用过对象"this")这是我的代码,谢谢!:
爪哇岛:
(function(){
var oForm = document.forms;
oForm[0].querySelector("input[type='radio']").
addEventListener("click",
sommeButton,
false);
}) ()
function sommeButton () {
var aButton = document.forms[0].r1;
}
和 html :
<html>
<head>
<meta charset="UTF-8">
<title>Exercise 5</title>
<link rel="stylesheet" href="css/form.css" />
</head>
<body>
<section>
<form name="frm1">
<label>
<input type="radio"
value="1"
name="r1" />
</label>
<label>
<input type="radio"
value="2"
name="r1" />
</label>
<label>
<input type="radio"
value="3"
name="r1" />
</label>
<label>
<input type="radio"
value="4"
name="r1" />
</label>
<label>
<input type="radio"
value="5"
name="r1" />
</label>
<label>
<input type="text" name="tEx2" />
</label>
</form>
</section>
</body>
</html>
此代码示例将引导您完成所询问的内容。 它将涵盖您的问题涉及的每个主题,包括:
- 收集文档中的元素
- 附加事件侦听器
- 在作用域中使用
this
- 获取和设置
<input>
元素的值
每个主题的详细说明作为注释包含在下面的相应代码旁边。
/*jslint browser:true*/
(function(doc) {
"use strict";
// This will be all of our logic for our program
function main() {
// Define our vars
var output, radios;
/*
* Now we need to collect the elements that we are going use
* We do that with documnet.getElementById() if the element
* has an id attribute that we know of. We can also use
* the querySelecter family to find elements with explicit
* ids. For example, we are going to look for all inputs whose
* type property is "radio", that means we get all the
* radio inputs in the document.
*/
// Find the input named "output" in our document
output = doc.getElementById("output");
// Get the radio inputs in our document
radios = doc.querySelectorAll("input[type='radio']");
// This function will set the value of the output element
function printValue() {
/*
* An important thing to note is that you have access to
* "output" here. The output variable was captured and is
* available in this funciton's scope.
*
* The other thing is that the "this" variable, in this
* instance, will be set to the radio button that the
* change event listener is attached to.
*/
// Make sure that this is the "checked" radio button
if (this.checked) {
// Set the "output" input's value to this radio's value
output.value = this.value;
}
}
// This is just a functor to use with the Array.prototype.forEach()
function setupRadio(radio) {
/*
* Here we actually attach the change event listener
* That means that whenever the radio changes, it will call
* the function that we attached.
*
* The nice thing about addEventListener() is that whenever
* it calls your function, it sets the scope to whatever the
* listener was attached to.
*
* In other words, it calls "printValue" and sets the "this"
* variable to radio. because of that, you have access to
* all of radio's properties without having to explicitly
* pass the radio to the printValue function.
*/
// Attach the change listener
radio.addEventListener("change", printValue);
}
/*
* NodeLists don't have a forEach method, so we borrow Array's
* This lets us iterate over all of the radios that we selected
* and call "setupRadio" on each one.
*/
Array.prototype.forEach.call(radios, setupRadio);
}
// Here we wait for the document to finish loading then call the "main" function
doc.addEventListener("DOMContentLoaded", main);
}(document));
main {
font-family: sans-serif;
}
fieldset {
padding: 0;
border: 0;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
}
<main>
<label for="output">Current Radio Value</label>
<input id="output" type="text">
<fieldset>
<legend>Radio Choices</legend>
<ul>
<li>
<label>
<input type="radio" name="which" value="First">
<span>First</span>
</label>
</li>
<li>
<label>
<input type="radio" name="which" value="Second">
<span>Second</span>
</label>
</li>
</ul>
</fieldset>
</main>
相关文章:
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- Sails.js:同时发布文本输入和一个文件
- 来自文本输入null的html javascript变量
- 当没有文本输入聚焦时检测空格键按下
- JS中的按钮和文本输入
- JavaScript:在调用函数的文本输入上按enter键
- jQuery自动完成功能不适用于多个文本输入
- 使用JS从选择和文本输入中捕获值,并将输出返回到HTML
- 如何通过jQuery发送选定的元素和文本输入
- HTML如何根据javascript函数的返回值限制文本输入
- jquery插件或javascript方法自动调整文本输入(而非文本区域)(固定宽度)可变高度的大小
- 在jquery中获取文本输入val始终为空
- HTML文本输入大小
- 文本输入与文本区域
- 角度ui选择标记模糊时丢失文本输入
- 文本输入值中的JavaScript变量
- Angular指令中的最佳实践是将文本输入设置为英尺和英寸的格式
- 如何在不通过模型验证的情况下屏蔽文本输入中输入的字符
- 在文本输入区域中创建双向更新
- 不使用自定义CSS或HTML(使用框架方法)的角度材质文本输入或文本区域标签大小