我有Java Script,这个创建复选框,我希望使用JQuery获取那些选中的复选框值。在我的java脚本中,我用复选框绑定学生的主题。当学生选择自己的科目时,我想获得这些科目列表。任何解决方案,请....
我的Java脚本是
<script type="text/javascript">
function specilizationChange()
{
$("#Subjectfield").empty();
var URL = "/SemesterRegistation/GetSubjects/";
$.ajax({
url: URL,
data: JSON.stringify({ SpecializationID: $("#SpecializationID").val() }),
cache: false,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
for (var i = 0; i < response.length; i++) {
$("#Subjectfield").append("<input type='checkbox' name='chksub' id='" + response[i].Subject + "'/><label>" + response[i].SubjectName + " </label>");
$("#Subjectfield").append("<input type='hidden' value='false' name='" + response[i].Subject + "' />")
}
},
error: function (reponse) {
}
});
}
</script>
答案 0 :(得分:0)
尝试这种方式使用复选框的公共类,然后使用此脚本获取已检查的ID
$("#Subjectfield").append("<input type='checkbox' class='sel_subj' name='chksub' id='Example'/><label>Example</label>");
$('#Subjectfield').on('change','.sel_subj',function(){
if($(this).is(':checked'))
alert($(this).attr('id'))
});
答案 1 :(得分:0)
我认为你应该改变你的javascript方法,如下所示:
function specilizationChange() {
$("#Subjectfield").remove();
var URL = "/SemesterRegistation/GetSubjects/";
$.ajax({
url: URL,
data: JSON.stringify({ SpecializationID: $("#SpecializationID").val() }),
cache: false,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json"
}).success(function (response) {
for (var i = 0; i < response.length; i++) {
var html = '<li>' +
'<input type="checkbox" name="chksub" class="class" value="' + response[i].Subject + '"/>' +
'<input type="hidden" name="' + response[i].Subject + '" value="false"/>' +
'</li>';
$('#Subjectfield').append(html);
}
}).fail(function() {
});
}
然后您可以将您的每个主题ID连接起来,如下所示:
$('#Subjectfield').on('change', 'input[class]', function () {
console.log($(this).closest('li').children('input').attr('value'));
});