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