簡體   English   中英

如果定義了隱藏輸入字段的值,該如何顯示?

[英]How can I show the value of a hidden input field if the value is defined?

 $(document).ready(function() { $(".city_name").click(function() { city = this.id; course = $("#courses").val(); course_type = $("#courses_type").val(); course_type2 = $("#courses_type2").val(); course_type3 = $("#courses_type3").val(); alert(course); alert(courses_type); alert(courses_type2); alert(courses_type3); alert(city); $.ajax({ type: "POST", data: { "id": city, "courses": course, "courses_type": course_type, "courses_type2": course_type2, "courses_type3": course_type3 }, url: "filter-city-colleges.php", success: function(data) { alert(data); } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="city_name"> Click me <div> <input type="hidden" id="courses_type" value="field"> <input type="hidden" id="courses_type2" value="univer"> <input type="hidden" id="courses_type3" value="course"> 

在此代碼中,當我在jquery中警告courses_type,courses_type2,courses_type3時,它顯示[objectHTMLcollection]。 如何獲取隱藏輸入框的值。

如果在變量前不使用var關鍵字,它將在全局空間中定義

$(document).ready(function(){
    $(".city_name").click(function(){
    var city=this.id;
    course = $("#courses").val(); // cannot find any DOM with id courses
    var course_type = $("#courses_type").val();
    alert(course);    // It will alert undefined since there is no DOM with id course

    alert(courses_type); // will alert field
    var course_type2 = $("#courses_type2").val();
    var course_type3 = $("#courses_type3").val();
    // put the alert here after retriving the value from DOM, before retrieving it will be undefined
    alert(courses_type2);
    alert(courses_type3);
    alert(city);
    $.ajax({
        // rest of the ajax
      });
    });
});

嘗試以下代碼,將所有帶有值的隱藏字段傳遞給ajax請求:

$(document).ready(function(){
        $(".city_name").click(function(){
            city=this.id;
            course = $("#courses").val();

            var data = {};
            $('input[type="hidden"]').each(function(){
                if($(this).val()){
                    data[$(this).attr('id')] = $(this).val();
                }
            });
            data['id'] = city;
            data['course'] = 'course';

            $.ajax({
                type:"POST",
                data:data,
                url:"filter-city-colleges.php",
                success:function(data){
                    alert(data);
                }

            });
        });
    });

您需要在alert()函數中提及正確的變量名,例如,您有一個名為course_type的變量,但您在警報中提到了course**s**_type 只是改變它:)

 $(document).ready(function() { $(".city_name").click(function() { city = this.id; course = $("#courses").val(); course_type = $("#courses_type").val(); course_type2 = $("#courses_type2").val(); course_type3 = $("#courses_type3").val(); // alert(course); alert(course_type); alert(course_type2); alert(course_type3); alert(city); $.ajax({ type: "POST", data: { "id": city, "courses": course, "courses_type": course_type, "courses_type2": course_type2, "courses_type3": course_type3 }, url: "filter-city-colleges.php", success: function(data) { alert(data); } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="city_name" id="cityId"> Click me <div> <input type="hidden" id="courses_type" value="field" /> <input type="hidden" id="courses_type2" value="univer" /> <input type="hidden" id="courses_type3" value="course" /> 

暫無
暫無

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

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