[英]Change input text based on radio button click in external javascript file
This is my code: 这是我的代码:
<div id="title">
<label class="" id="title-text" for="title">Hello World</label>
<input type="text" name="doc-title" value="" id="title" autocomplete="off">
</div>
<div id="title-select">
<input type="radio" name="title_format" class="title-format" id="title-format-0" value="0" checked="checked"><label for="title-format-0">Title 1</label><br>
<input type="radio" name="title_format" class="title-format" id="title-format-1" value="1"> <label for="title-format-1">Title 2</label><br>
<input type="radio" name="title_format" class="title-format" id="title-format-2" value="2"> <label for="title-format-2">Title 3</label>
</div>
What i need is, 我需要的是
when a radio item is clicked, it should display the respective title. 单击单选项目时,它应显示相应的标题。 Say when radio button 2 is clicked, "Title 2" should get updated for label and input elements above.
例如,当单击单选按钮2时,应该为上面的标签和输入元素更新“标题2”。
This needs to be done in an EXTERNAL JS FILE! 这需要在外部JS文件中完成!
The 2 DIV blocks mentioned above are actually placed in separate PHP files and are heavily cascaded. 上面提到的2个DIV块实际上放置在单独的PHP文件中,并且进行了级联。 So need a pure JS fix.
因此需要一个纯JS修复程序。
Currently what i have in my external file is this (ext file is getting called properly): 目前我在外部文件中拥有的是此文件(外部文件被正确调用):
$('input[name=title_format]').click(function() {
$('input[name=doc-title]').value('Title changed to 2');
};
Ofcourse this code seems completely useless. 当然,这段代码似乎完全没有用。 Hope somebody helps me here.
希望有人在这里帮助我。
PS: I am an absolute noob in js/jquery so please excuse if this is a simple fix. PS:我是js / jquery中的绝对菜鸟,因此,如果这是一个简单的解决方法,请原谅。 I would prefer simple javascript fix for this if possible.
如果可能的话,我希望对此进行简单的JavaScript修复。 Else JQuery, which i understand is by including jquery-2.1.1.min.js file before my js file.
我了解的其他JQuery是,在我的js文件之前包含jquery-2.1.1.min.js文件。
try this: html: 试试这个:html:
<div id="title">
<label class="" id="title-text" for="title">Hello World</label>
<input type="text" name="doc-title" value="" id="title" autocomplete="off">
</div>
<div id="title-select">
<input type="radio" name="title_format" value="title3" onclick="changeTheText(this.value);" class="title-format" id="title-format-0" value="0" checked="checked"><label for="title-format-0">Title 1</label><br>
<input type="radio" name="title_format" value="title3" onclick="changeTheText(this.value);" class="title-format" id="title-format-1" value="1"> <label for="title-format-1">Title 2</label><br>
<input type="radio" name="title_format" value="title3" onclick="changeTheText(this.value);" class="title-format" id="title-format-2" value="2"> <label for="title-format-2">Title 3</label>
</div>
javascript: JavaScript的:
function changeTheText(theValue)
{
document.getElementById('title').value = theValue;
}
You forgot to close the click function. 您忘记了关闭点击功能。 Also make a document ready function.
同时使文档准备就绪功能。 Or else it does not know which element its about.
否则,它不知道它与哪个元素有关。
$(document).ready(function(){
$('[name=title_format]').click(function() {
$('[name=doc-title]').val('Title changed to '+$(this).val());
});
});
EDIT: now changes the input text to the value of the radio button. 编辑:现在将输入文本更改为单选按钮的值。
Assuming the files are loading correctly, try ... 假设文件正确加载,请尝试...
Change .value
to .val
将
.value
更改为.val
here is your html code(just added jquery cdn and external JS file) 这是您的html代码(仅添加了jQuery CDN和外部JS文件)
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="jack.js"></script>
<div id="title">
<label class="" id="title-text" for="title">Hello World</label>
<input type="text" name="doc-title" value="" id="title" autocomplete="off">
</div>
<div id="title-select">
<input type="radio" name="title_format" class="title-format" id="title-format-0" value="0" checked="checked"><label for="title-format-0">Title 1</label><br>
<input type="radio" name="title_format" class="title-format" id="title-format-1" value="1"> <label for="title-format-1">Title 2</label><br>
<input type="radio" name="title_format" class="title-format" id="title-format-2" value="2"> <label for="title-format-2">Title 3</label>
</div>
and your external javascript code jack.js 和您的外部javascript代码jack.js
$(document).ready(function(){
$("input:radio[name=title_format]").click(function() {
var idVal = $(this).attr("id");
var value = $("label[for='"+idVal+"']").text();
$('input[name=doc-title]').val(value);
});
});
Is correct to use .val
. 正确使用
.val
。
In your example there is an error at the end JS, you miss a )
. 在您的示例中,JS结尾出现错误,您错过了
)
。
try this
尝试这个
js file
js文件
$(document).ready(function(){
$('#title-select .title-format').click(function() {
var title=$(this).next().text();
$('#title :input').val(title);
});
})
html script registration
html脚本注册
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.js"></script>
<script type="text/javascript" src="js-file.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.