我在页面中有以下单选按钮具有引导程序和jquery-ui:
<input type="radio" name="activeSolution" value="48">
...
<input type="radio" name="activeSolution" value="50">
...
<input type="radio" name="activeSolution" value="10">
我试图从jQuery自动完成选择事件中获取已检查的单选按钮索引,如下所示:
....
select: function( event, ui ) {
active = $("input:radio[name='activeSolution']:checked").index();
alert($("input[name='activeSolution']:checked").index());
alert(active)
....
始终alert(active)
返回0
我所做的几乎就像this answer。
我还尝试将元素提供给index()
,如下所示:
active = $("input:radio[name='activeSolution']");
alert(active.index(active.find(":checked")));
但是,这会返回-1
编辑:添加jQuery ui的完整代码上下文:
<script>
$( function() {
$( "#solutions" ).autocomplete({
source: function( request, response ) {
console.log(request.term)
$.ajax( {
url: "/solution/autocomplete/"+request.term,
dataType: "json",
/* data: {
term: request.term
},*/
success: function( data ) {
response( data );
}
} );
},
minLength: 2,
select: function( event, ui ) {
/* log( "Selected: " + ui.item.label + " aka " + ui.item.value );*/
active = $("input:radio[name='activeSolution']:checked").index();
alert($("input[name='activeSolution']:checked").index());
alert(active)
//alert($("input.activeSolution:checked").index());
/* active = $("input:radio[name='activeSolution']");
alert(active.index(active.find(":checked")));
alert(active)
alert(active.index(active.find(':checked')))*/
this.value = ui.item.label;
console.log(this)
li = '<li class="list-group-item">'+ui.item.label+'<input type="hidden" name="solutions[][]" value="'+ui.item.value+'"><button type="button" class="btn btn-xs btn-danger badge remove"><i class="glyphicon glyphicon-minus"></i></button></li>';
notFound = true;
$(".solutions-list li input[name='solutions["+this.title+"][]']").each(function(i){
//alert($(this).val()+"\n"+ui.item.value)
if ($(this).val() == ui.item.value){
alert('{{__('This cause is already selected!')}}');
notFound = false;
return false;
}
});
if (notFound) $("#solutions-list").prepend(li);
return false;
}
} );
} );
</script>