[英]How can I poupulate a hidden form field with a value from a dropdown?
I have these: 我有这些:
$(document).ready(function() {
getRequestCategories();
$('#requestCategory').change(function() {
getRequestDescriptions( $(this).val() );
});
});
function getRequestCategories() {
$.ajax({
url: 'getCategories.php',
dataType: 'json'
})
.done(function(categoryInfo) {
$(categoryInfo).each(function(i, category) {
$('<option>').val(category.RequestCategoryDisplay).text(category.RequestCategoryDisplay).appendTo( $('#requestCategory') );
})
});
}
function getRequestDescriptions(requestCategory) {
$.ajax({
url: 'getDescriptions.php',
dataType: 'json',
data: { requestCategory: requestCategory }
})
.done(function(descriptionInfo) {
$(descriptionInfo).each(function(i, description) {
$('<option>').val(description.RequestDescriptionDisplay).text(description.RequestDescriptionDisplay).appendTo( $('#description') );
})
});
}
Category
<select name="requestCategory" id="Category" style="width:250px;font-size:10pt;" class="changeable" data-summary="summCategory">
<option value=""></option>
</select>
Description
<select name="description" id="description" style="width:250px;font-size:10pt;" class="changeable" data-summary="summSubCategory">
<option value=""></option>
</select>
How it works currently: 目前如何运作:
When you select a value from the Category
dropdown, all the values associated with your selection are automatically into the description
dropdown. 当您从
Category
下拉列表中选择一个值时,与您的选择关联的所有值将自动添加到description
下拉列表中。
This works fine. 这很好。
However, we have a new requirement to populate the following hidden form field with user's selection from the description
dropdown: 但是,我们有一个新要求,即使用用户从
description
下拉列表中进行的选择来填充以下隐藏的表单字段:
<input name="RequestID" id="RequestID" type="text" width="300" value="" class="changeable" />
In other words, once the description
dropdown is populated with values based on selection from Category
dropdown, then when a user selects one of the values from the description
dropdown, the accompanying value of RequestID should be saved into a hidden form field. 换句话说,一旦使用基于“
Category
下拉列表中的选择填充值的description
下拉列表,则当用户从description
下拉列表中选择一个值时,RequestID的随附值应保存到隐藏的表单字段中。
I tried modifying the getDescriptions function but I am not getting the correct values. 我尝试修改getDescriptions函数,但没有获得正确的值。
Each value I select from the description dropdown gives me same values for RequestID. 我从描述下拉列表中选择的每个值都为我提供了相同的RequestID值。
Can you please see what I am doing wrong? 你能看看我在做什么错吗?
Thanks so much in advance. 非常感谢。
function getDescriptions(requestCategory) {
$.ajax({
url: 'getDescriptions.php',
dataType: 'json',
data: { requestCategory: requestCategory }
})
.done(function(descriptionInfo) {
// get number of items in array given by php
var Desc_count = descriptionInfo.length;
// loop request descriptions
for (var i = 0; i < Desc_count; i += 1) {
// append an <option> tag to your <select>
$('#description').append('<option value="' + descriptionInfo[i].RequestID + '">' + descriptionInfo[i].RequestDescriptionDisplay + '</option>');
}
// Listen for the value of the <select> to change
$('#description').on('change', function () {
// get the value of the selected option ,the value is the descriptionInfo[i].RequestID
var value = $( "#description option:selected").val();
// Set the value of the hidden fields based on the <select>'s ID choosing the corret array element
$('input[name="RequestID"]').val(value);
});
});
}
Very strange this isnt working.. is your on change event happening in $(document).ready() ? 非常奇怪,这不起作用..您的on change事件是否在$(document).ready()中发生?
I try to help: 我尝试帮助:
instad of your $('#description').on('change', function () {
event try this: $('#description').on('change', function () {
事件的尝试安装:
$(document).ready(function() {
$('#description').change(function() {
$('#RequestID').val($(this).val());
});
});
if this doesnt work try: 如果这不起作用,请尝试:
$(document).ready(function() {
$('#description').live('change', function() {
$('#RequestID').val($(this).val());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.