简体   繁体   English

如何使用下拉菜单中的值填充隐藏的表单字段?

[英]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.

相关问题 如何从表单上的隐藏字段中提取值 - How to extract value from hidden field on form 如何将下拉选择中的隐藏值传递给表单? - How to pass hidden value from dropdown selection to a form? 如何在表单查找期间将隐藏的 SQL ID 字段值与其他数据一起传递? - How can I pass a hidden SQL ID field value alongside other data during a form lookup? 如果定义了隐藏输入字段的值,该如何显示? - How can I show the value of a hidden input field if the value is defined? 如何将值从Jquery传递给PHP? HTML是隐藏的输入表单字段 - How do I pass value from Jquery to PHP ? HTML is a hidden input form field JavaScript - 如何从函数传递/存储值以形成隐藏字段? - JavaScript - How to pass/store value from function to form hidden field? 如何从我的第一页复制这个隐藏的值并在我的第二页发布? - How can i copy this hidden value in field from my first page and post in my 2nd page? 如何在不使用隐藏字段值单击按钮的情况下从jQuery中的MySQL中获取数据? - How can I fetch data from MySQL in jQuery without a click of a button using an hidden field value? 如何将“值”字段的值放入数据库中输入类型为复选框的形式? - how can i put the value of “value” field in a form where input type is checkbox from database? 如何将循环值从选择表单中的隐藏输入字段传递给Ajax - How to pass loop value from hidden input field within select form to Ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM