使用 javascript 在下拉列表更改时填充表单字段
我正在尝试根据下拉框中选择的值填充字段中的数据。我已将下拉选项选择传递给 javascript 函数,但在更新表单字段时遇到问题。请检查我的代码是否有我所犯的错误。
这是我的 html 代码:
<div class="form-ui">
<span class="form-elements reqField">
<select class="dropdown" onchange="populateData(this.options[this.selectedIndex].value,1)">
<option value="1">Please Select</option>
<option value="2">Mrs.Jane Smith</option>
<option value="3">Mr.Junior Smith</option>
</select>
</span>
<span class="form-elements reqField">
<select id="itle1" class="dropdown">
<option value="1">Mr.</option>
<option value="2">Ms.</option>
<option value="3">Mrs.</option>
</select>
</span>
<span class="form-elements">
<input id="FirstName1" type="text" class="text" value="" />
</span>
<span class="form-elements reqField">
<input id="LastName1" type="text" class="text" value="" />
</span>
</div>
和 javascript 代码:
function populateData(value,person){
if(value==2){
$('#Title'+person).value=3;
$('#FirstName'+person).value="Jane";
$('#LastName'+person).value="Smith";
}
if(value==3){
}
}
I am trying to populate data in my fields depending on the value selected in the drop down box. I have passed the dropdown option select to the javascript function, but having problem in updating the form fields. Please check my code for the error I've committed.
This is my html code:
<div class="form-ui">
<span class="form-elements reqField">
<select class="dropdown" onchange="populateData(this.options[this.selectedIndex].value,1)">
<option value="1">Please Select</option>
<option value="2">Mrs.Jane Smith</option>
<option value="3">Mr.Junior Smith</option>
</select>
</span>
<span class="form-elements reqField">
<select id="itle1" class="dropdown">
<option value="1">Mr.</option>
<option value="2">Ms.</option>
<option value="3">Mrs.</option>
</select>
</span>
<span class="form-elements">
<input id="FirstName1" type="text" class="text" value="" />
</span>
<span class="form-elements reqField">
<input id="LastName1" type="text" class="text" value="" />
</span>
</div>
And the javascript code:
function populateData(value,person){
if(value==2){
$('#Title'+person).value=3;
$('#FirstName'+person).value="Jane";
$('#LastName'+person).value="Smith";
}
if(value==3){
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jQuery 使用
val()
来获取和设置值。所以你的js函数应该是
而不是
Also
应该是
jQuery uses
val()
to get and set values.So your js function should be
and not
Also
<select class="dropdown" onchange="populateData(this.options[this.selectedIndex].value,1)">
should be
<select class="dropdown" onchange="populateData(this.value,1)">
快速回答是,我认为您需要使用 val() 而不是 value - 不太记得它如何与 select 一起使用
http://api.jquery.com/val/
quick answer is that I think you need to use val() not value - can't quite remember how it works with select though
http://api.jquery.com/val/
此处出现 HTML ID 错误
另外我认为 javascript 应该看起来像这样
HTML ID error here
<select id="itle1" class="dropdown">
Should be
Title1
notitle1
Also I think the javascript should look like this
您可以使用 FORM 代替 DIV,使用属性
name
代替id
。它们很久以前就被设计用来管理表单。
您可以使用 jQuery 来实现这一点,但是一切都已经内置在纯 Javascript 中来处理表单。
You could use a FORM instead of a DIV, and the attribute
name
, instead ofid
.They were designed a long time ago to manage forms.
You can use jQuery for that, but everything is already built-in in pure Javascript to handle forms.