我的 View 中有一个下拉列表、一个提交按钮和一个文本框。我想在单击提交按钮或 dropdownlist 的 onChange 事件时将 dropdownlist 的选定值传递到文本框。我怎样才能实现这个目标???
我解决的方法如下:
<script type="text/javascript">
$(function() {
$('#ddlComp').change(function() {
var selectedValue = $(this).val();
$('#txtCompName').val(selectedValue);
});
});
</script>
<div>
@Html.DropDownList("ddlcomp", Model.CompanyList)
<input type="submit" value="Submit" />
@Html.TextBox("txtCompName")
</div>
最佳答案
I want to pass the selected value of dropdownlist to the textbox when the submit button is clicked or onChange event of dropdownlist
$(function() {
var selectedValue = ''; // declare variable here
// on drop down change
$('#ddlComp').change(function() {
selectedValue = $(this).val(); // store value in variable
$('#txtCompName').val(selectedValue); // update on change
});
// on submit button click
$('input[type=submit]').click(function(){
$('#txtCompName').val(selectedValue); // update on submit button
});
});
关于jquery - 将下拉列表的值传递到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11070637/