簡體   English   中英

如何將onchange選擇的下拉值存儲到隱藏的輸入字段中?

[英]How to store onchange selected dropdown value into hidden input field?

這是我的ajax jQuery代碼,在其中我完成了下拉填充的工作並將onchange id存儲到隱藏的輸入字段中。為什么它不存儲到id字段中? 我檢查警報是否值即將到來。 價值即將到來,但它沒有存儲到隱藏字段中。

<script type="text/javascript">
$(document).ready(function() {
     $("#village").change(function(){
            $('#hiddenVillage').val( $(this).val());// this code to store id into this field
    }); 
    $('#taluka').change(function() 
            {
            var myObject = {talukaid: $(this).val(),districtid: $('#district').val(), state_id:$('#stateDrop').val()};
            var cpytaluka= $(this).val();
            $('#hiddenTaluka').val($(this).val(cpytaluka));// this code to store id into this field
            $.ajax({  
                  type : "get",   
                  url : "villages.htm",   
                  data : myObject,
                  success : function(response) { 
                    $('#village').append(response);
             },  
                 error : function(e) {  
                  alert('Error: ' + e);   
                 }
            });
    });
     $('#district').change(function() 
                {
                var myObject = {districtid: $(this).val(), state_id:$('#stateDrop').val()};
                var cpydistrict= $(this).val();
                $("#hiddenDistrict").val($(this).val(cpydistrict));// this code to store id into this field
                $.ajax({  
                      type : "get",   
                      url : "taluka.htm",   
                      data : myObject,
                      success : function(response) { 
                        $('#taluka').append(response);
                 },  
                     error : function(e) {  
                      alert('Error: ' + e);   
                     }

                });

        });
         $('#stateDrop').change(function() 
        {
                     var state = $(this).val();
                    $("#hiddenstate").val(state);// this code to store id into this field
                      $.ajax({  
                      type : "get",   
                      url : "district.htm",   
                      data : "State_ID=" + state,  
                      success : function(response) {  
                        $('#district').append(response);
                 }, 
                     error : function(e) {  
                      alert('Error: ' + e);   
                     }
                });
            }); 
        });
</script>

這是我的表格,其中我在每個下拉字段下方都包含輸入字段

<form action="search-Leaders-list" id="searchleader" method="GET">
    <div class="row">
        <div class="form-group col-lg-3">
            <select id="stateDrop"
                title="State &lt;i class=&quot;fa fa-angle-down&quot;&gt;&lt;/i&gt;">
                <option value="small">State</option>
                <c:forEach items="${stateList}" var="state">
                    <option value="${state.state_Id}">${state.state_Name}</option>
                </c:forEach>
            </select> <input type="hidden" id="hiddenstate">
        </div>
        <div class="form-group col-lg-3">
            <select id="district">
                <option value="small">District</option>
            </select> <input type="hidden" id="hiddenDistrict">
        </div>
        <div class="form-group col-lg-2">
            <select id="taluka">
                <option value="small">Taluka</option>
            </select> <input type="hidden" id="hiddenTaluka">
        </div>
        <div class="form-group col-lg-2">
            <select id="village"
                title="Villages &lt;i class=&quot;fa fa-angle-down&quot;&gt;&lt;/i&gt;">
                <option value="small">Villages</option>
            </select> <input type="hidden" id="hiddenVillage">
        </div>
        <div class="form-group col-lg-2">
            <input type="submit" value="Search" class="submit">
        </div>
    </div>
</form>

請幫助我。

我真的不明白你的問題。 如果要獲取此下拉框的ID並將其保存在隱藏字段中。

$('#taluka').change(function() 
        {

        $('#hiddenTaluka').val(this.id);

或使用

 $('#hiddenTaluka').val($(this).attr('id'));

如果要存儲Dropbox所選項目的值。

 var cpydistrict= $(this).val();
            $("#hiddenDistrict").val(cpydistrict);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM