jQuery i-Check 插件相同 class 名称 - 检索两个不同的值
jQuery i-Check plugin same class name - retrieving two different values
我正在使用 I-check 插件,我正在尝试从代表两个不同字段的两个输入字段中检索值。
Html:
<label><input id="hoteltype" class="i-check" type="checkbox" name="hotel" value="3"/>Hotel</label>
<label><input id="starrating" class="i-check" type="checkbox" name="1" value="1.0" />1 star</label>
jQuery.ajax
<script>
$('.i-check input').on('ifChanged', function(event){
$('.searchtable').addClass('hide');
$('.spinner').removeClass('hide');
$.ajax({
type: 'GET',
data: {'name':'<?php echo strval($_GET['name']); ?>','arrival':'<?php echo strval($_GET['arrival']); ?>','departure':'<?php echo strval($_GET['departure']);?>','guests':'<?php echo strval($_GET['guests']);?>','propertyCategory':$(".i-check input[type='checkbox']:checked").val(),'minStarRating':$(".i-check input[type='checkbox']:checked").val(),'order_by':$('#order_by').val()},
url: '<?php echo $baseUrl ?>/hotels/hotelFilterResult.php',
success: function (data) {
//alert('data loaded succesfully');
alert(this.url);
$('.searchtable').replaceWith(data);
$('.spinner').addClass('hide');
$('.searchtable').removeClass('hide');
},
error: function (xhr) {
alert('data not loaded');
}
});
});
问题不是检索值,而是分离值的问题,因为我无法更改 classname。
我通过
检索值
'minStarRating':$(".i-check input[type='checkbox']:checked").val()
'hoteltype':$(".i-check input[type='checkbox']:checked").val()
现在的问题是我在两种不同的表示中得到了相同的值。
例如,如果选择星级 - 我在这里得到的值都是 1.0,但实际上它在酒店类型中应该是空的。问题在于标识符“.i-check”。我试图向两者添加一个 ID class,所以一个 ID="starrating",另一个 ID="hoteltype",然后这样做:
'minStarRating':$(".i-check#starrating input[type='checkbox']:checked").val()
'hoteltype':$(".i-check#hoteltype input[type='checkbox']:checked").val()
这似乎不适用于 I-Check 插件,我该如何解决才能将两个值分开?
如果在 id
和元素名称(在本例中为 input
之间留下 space,浏览器认为 input
元素是后代id
元素;所以我认为最好在这两者之间省略 spaces。 More info about ID selectors。以下是一些可用于查找相关值的替代方法。
$("label").on("click", "input:checkbox", function() {
var result = 'minStarRating ' + $("input[type='checkbox']:checked#starrating").val();
var result2 = 'hoteltype ' + $("input[type='checkbox']:checked#hoteltype").val();
// same as above but in different order
// var result = 'minStarRating ' + $("#starrating:input[type='checkbox']:checked").val();
// var result2 = 'hoteltype ' + $("#hoteltype:input[type='checkbox']:checked").val();
//the below works too but it's longer, it uses also class names
//var result = 'minStarRating ' + $(".i-check#starrating:input[type='checkbox']:checked").val();
//var result2 = 'hoteltype ' + $(".i-check#hoteltype:input[type='checkbox']:checked").val();
$("#result").text(result);
$("#result2").text(result2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input id="hoteltype" class="i-check" type="checkbox" name="hotel" value="3" />Hotel</label>
<label>
<input id="starrating" class="i-check" type="checkbox" name="1" value="1.0" />1 star</label>
<div id="result"></div>
<div id="result2"></div>
我不确定我是否回答了你的问题,因为我以前没有使用过 iCheck(尽管它看起来非常有趣,风格也很棒),但我希望这对你有所帮助:-)
我正在使用 I-check 插件,我正在尝试从代表两个不同字段的两个输入字段中检索值。
Html:
<label><input id="hoteltype" class="i-check" type="checkbox" name="hotel" value="3"/>Hotel</label>
<label><input id="starrating" class="i-check" type="checkbox" name="1" value="1.0" />1 star</label>
jQuery.ajax
<script>
$('.i-check input').on('ifChanged', function(event){
$('.searchtable').addClass('hide');
$('.spinner').removeClass('hide');
$.ajax({
type: 'GET',
data: {'name':'<?php echo strval($_GET['name']); ?>','arrival':'<?php echo strval($_GET['arrival']); ?>','departure':'<?php echo strval($_GET['departure']);?>','guests':'<?php echo strval($_GET['guests']);?>','propertyCategory':$(".i-check input[type='checkbox']:checked").val(),'minStarRating':$(".i-check input[type='checkbox']:checked").val(),'order_by':$('#order_by').val()},
url: '<?php echo $baseUrl ?>/hotels/hotelFilterResult.php',
success: function (data) {
//alert('data loaded succesfully');
alert(this.url);
$('.searchtable').replaceWith(data);
$('.spinner').addClass('hide');
$('.searchtable').removeClass('hide');
},
error: function (xhr) {
alert('data not loaded');
}
});
});
问题不是检索值,而是分离值的问题,因为我无法更改 classname。
我通过
检索值'minStarRating':$(".i-check input[type='checkbox']:checked").val()
'hoteltype':$(".i-check input[type='checkbox']:checked").val()
现在的问题是我在两种不同的表示中得到了相同的值。
例如,如果选择星级 - 我在这里得到的值都是 1.0,但实际上它在酒店类型中应该是空的。问题在于标识符“.i-check”。我试图向两者添加一个 ID class,所以一个 ID="starrating",另一个 ID="hoteltype",然后这样做:
'minStarRating':$(".i-check#starrating input[type='checkbox']:checked").val()
'hoteltype':$(".i-check#hoteltype input[type='checkbox']:checked").val()
这似乎不适用于 I-Check 插件,我该如何解决才能将两个值分开?
如果在 id
和元素名称(在本例中为 input
之间留下 space,浏览器认为 input
元素是后代id
元素;所以我认为最好在这两者之间省略 spaces。 More info about ID selectors。以下是一些可用于查找相关值的替代方法。
$("label").on("click", "input:checkbox", function() {
var result = 'minStarRating ' + $("input[type='checkbox']:checked#starrating").val();
var result2 = 'hoteltype ' + $("input[type='checkbox']:checked#hoteltype").val();
// same as above but in different order
// var result = 'minStarRating ' + $("#starrating:input[type='checkbox']:checked").val();
// var result2 = 'hoteltype ' + $("#hoteltype:input[type='checkbox']:checked").val();
//the below works too but it's longer, it uses also class names
//var result = 'minStarRating ' + $(".i-check#starrating:input[type='checkbox']:checked").val();
//var result2 = 'hoteltype ' + $(".i-check#hoteltype:input[type='checkbox']:checked").val();
$("#result").text(result);
$("#result2").text(result2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input id="hoteltype" class="i-check" type="checkbox" name="hotel" value="3" />Hotel</label>
<label>
<input id="starrating" class="i-check" type="checkbox" name="1" value="1.0" />1 star</label>
<div id="result"></div>
<div id="result2"></div>
我不确定我是否回答了你的问题,因为我以前没有使用过 iCheck(尽管它看起来非常有趣,风格也很棒),但我希望这对你有所帮助:-)