将文本从选择字段复制到文本字段的Javascript [英] Javascript to copy text from select field to text field
本文介绍了将文本从选择字段复制到文本字段的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将值从选择字段复制到文本字段
I am trying to copy the values from a select field to a text field
我碰到了这个
但是当我使用以下内容创建测试页时,该页面无法正常工作,显然我把它放错了:(
But when I created a test page using the following it does not work, I have obviously put it together wrong :(
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="//code.jquery.com/jquery-compat-git.js"></script>
<title>Test copy select</title>
<script type='text/javascript'>
$('select').change(function(){
$('input[type=name]').val($('option:selected',this).text());
$('input[type=price]').val($(this).val());
});
</script>
</head>
<body>
<form>
<select>
<option value="10">Apple</option>
<option value="20">Orange</option>
</select>
<input type="name" />
<input type="price" />
</form>
</body>
</html>
我该如何解决?
谢谢
推荐答案
将您的JavaScript包裹在$(document).ready()
中:
Wrap your javascript in $(document).ready()
:
$(document).ready(function() {
$('select').change(function(){
$('input[type=name]').val($('option:selected',this).text());
$('input[type=price]').val($(this).val());
});
});
有关进一步的说明,请参见这篇文章.
See this post for further explanation.
这篇关于将文本从选择字段复制到文本字段的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文