Javascript 需要将id追加/删除到“上的隐藏字段”;更改“;
我在我的页面中有很多复选框,每次我点击一个复选框,我就会得到我感兴趣的id,然后填写一个隐藏的表单。我的脚本正在工作,但是如果我取消选中复选框,它应该会从隐藏字段中删除id(现在它会在隐藏字段中再次追加id)。我怎样才能写出这样的剧本?以下是我目前的代码:Javascript 需要将id追加/删除到“上的隐藏字段”;更改“;,javascript,jquery,hidden-field,Javascript,Jquery,Hidden Field,我在我的页面中有很多复选框,每次我点击一个复选框,我就会得到我感兴趣的id,然后填写一个隐藏的表单。我的脚本正在工作,但是如果我取消选中复选框,它应该会从隐藏字段中删除id(现在它会在隐藏字段中再次追加id)。我怎样才能写出这样的剧本?以下是我目前的代码: $(document).ready(function() { var string = ""; $('.checkbox').on('change', function() { var subscription_id
$(document).ready(function() {
var string = "";
$('.checkbox').on('change', function() {
var subscription_id = $(this).parent().attr('id');
if(string == "") {
string = subscription_id;
} else {
string = string + ", " + subscription_id;
}
$('#select_players').val(string);
$('#subscription_ids_export').val(string);
})
})
您可以通过仅从选中的复选框中检索
subscription\u id
值来简化此过程,而不是每次添加/删除值
试试这个:
$('.checkbox').on('change', function() {
var subscriptionIds = $('.checkbox:checked').map(function() {
return $(this).parent().prop('id');
}).get().join(',');
$('#select_players').val(subscriptionIds);
$('#subscription_ids_export').val(subscriptionIds);
})
您可以通过仅从选中的复选框中检索
订阅id
值来简化此过程,而不是每次添加/删除值
试试这个:
$('.checkbox').on('change', function() {
var subscriptionIds = $('.checkbox:checked').map(function() {
return $(this).parent().prop('id');
}).get().join(',');
$('#select_players').val(subscriptionIds);
$('#subscription_ids_export').val(subscriptionIds);
})
您可以通过仅从选中的复选框中检索
订阅id
值来简化此过程,而不是每次添加/删除值
试试这个:
$('.checkbox').on('change', function() {
var subscriptionIds = $('.checkbox:checked').map(function() {
return $(this).parent().prop('id');
}).get().join(',');
$('#select_players').val(subscriptionIds);
$('#subscription_ids_export').val(subscriptionIds);
})
您可以通过仅从选中的复选框中检索
订阅id
值来简化此过程,而不是每次添加/删除值
试试这个:
$('.checkbox').on('change', function() {
var subscriptionIds = $('.checkbox:checked').map(function() {
return $(this).parent().prop('id');
}).get().join(',');
$('#select_players').val(subscriptionIds);
$('#subscription_ids_export').val(subscriptionIds);
})
非常好的方法,我想检查is(:checked),但这是better@DavidGeismar,为什么不起作用?提供更多细节。你看到小提琴了吗?我想它不起作用,因为在我的html中,
$('.checkbox:checked')
没有返回任何东西,因为我从来没有在html中看到选中的属性,即使复选框是checked@DavidGeismar你不该这么做。:checked
选择器作用于checked
属性,该属性在检查元素时隐式设置。非常好的方法,我想检查是(:checked),但这是better@DavidGeismar,为什么不起作用?提供更多细节。你看到小提琴了吗?我想它不起作用,因为在我的html中,$('.checkbox:checked')
没有返回任何东西,因为我从来没有在html中看到选中的属性,即使复选框是checked@DavidGeismar你不该这么做。:checked
选择器作用于checked
属性,该属性在检查元素时隐式设置。非常好的方法,我想检查是(:checked),但这是better@DavidGeismar,为什么不起作用?提供更多细节。你看到小提琴了吗?我想它不起作用,因为在我的html中,$('.checkbox:checked')
没有返回任何东西,因为我从来没有在html中看到选中的属性,即使复选框是checked@DavidGeismar你不该这么做。:checked
选择器作用于checked
属性,该属性在检查元素时隐式设置。非常好的方法,我想检查是(:checked),但这是better@DavidGeismar,为什么不起作用?提供更多细节。你看到小提琴了吗?我想它不起作用,因为在我的html中,$('.checkbox:checked')
没有返回任何东西,因为我从来没有在html中看到选中的属性,即使复选框是checked@DavidGeismar你不该这么做。:checked
选择器作用于选中元素时隐式设置的checked
属性。