如何从“ <p>”标签获取文本值?我无法使用jQuery方法从'<p>'标签获取文本

时间:2019-08-11 10:11:21

标签: jquery

alert("hi " + $("#time_in_"+csa_student_id).text());

我正在尝试使用此ID获取“输入时间”文本,但无法获取文本。

我已经检查了与我设置的ID正确的ID,但不确定为什么我的代码无法正常工作。

function getStudentAttendance(branch_id, class_id, session, date) {
  $.ajax({
    type: "GET",
    url: "{{URL::TO('/attendance/getStudentAttendance')}}/" + branch_id + "/" + class_id + "/" + session + "/" + date,
    cache: true,
    dataType: "JSON",
    success: function(response) {
      console.log('getStudentAttendance');
      console.log(response);
      $('#studentCard').empty();
      $('#noStudent').remove();
      if (response.length > 0) {
        for (i = 0; i < response.length; i++) {
          var student_id = response[i].student_id;
          var profile_image = response[i].profile_image;
          var student_name = response[i].name;
          var class_name = response[i].class_name;
          $('#studentCard').append(
            '<div class="col m4 s12 card attendance_card" id="attendance_card_' + student_id + '">' +
            '<img src="storage/profile_image/' + profile_image + '" height="150px" width="150px">' +
            '<p id="student_name_' + student_id + '" style="text-align:left; margin:0px; margin-top:10px;">Student Name: ' + student_name + '</p>' +
            '<p style="text-align:left; margin-top:0px; margin-bottom:0px; id="class_name_' + student_id + '">Class: ' + class_name + '</p>' +
            '<p style="text-align:left; margin-top:0px; margin-bottom:0px; id="time_in_' + student_id + '">Time In: 00:00:00</p>' +
            '<p style="text-align:left; margin-top:0px; margin-bottom:0px; id="time_out_' + student_id + '">Time Out: 00:00:00</p>' +
            '</div>'
          );
          //Ajax check student attendance
          $.ajax({
            type: "GET",
            url: "{{URL::TO('/attendance/checkStudentAttendance')}}/" + student_id + "/" + date,
            cache: true,
            // dataType:"JSON",
            success: function(response) {
              console.log('check_student_attendance: ' + response);
              // console.log(response);
              var status = response[0];
              var csa_student_id = response[1];
              var csa_time_in = response[2];
              console.log('' + csa_student_id);
              if (status == 'no_student_attendance') {
                // <a class="waves-effect waves-light btn modal-trigger" href="#modal1">Modal</a>
                $("#attendance_card_" + csa_student_id).append('<button type="button" data-target="modal1" id="sign_in_button" value="' + csa_student_id + '" class="waves-effect waves-light btn modal-trigger" style="width:100%;">Sign In</button>');
              } else if (status == 'signed_in') {
                $("#attendance_card_" + csa_student_id).css('background-color', '#00FA9A');
                // $("#time_in_"+csa_student_id).text();
                alert("hi " + $("#time_in_" + csa_student_id).text());
                // $("#attendance_card_"+csa_student_id).append('<p style="text-align:left; margin:0px; margin-bottom:5px;">Time In: '+ csa_time_in +'</p>');
                $("#attendance_card_" + csa_student_id).append('<button type="button" id="sign_out_button" value="' + csa_student_id + '" class="waves-effect waves-light btn" style="width:100%; bottom:0px;">Sign Out</button>');
                // $('#sign_out_button').val(csa_student_id);
              } else {
                //signed out
              }
            },
            error: function(obj, textStatus, errorThrown) {
              console.log("Error " + textStatus + ": " + errorThrown);
            }
          });
          //if no student record -->grey 
          //if status sign in (background green)       
          //if status sign out (background grey)
          //if no status (backgorund grey)
        }
      } else {
        $('#sign_in_attendance').append('<p id="noStudent">No Student Attendance</p>');
        // swal('no Student Record');
      }
      // var student_id = $('#collection_student_name').val();
      // $('#collection_student_name').trigger('contentChanged');
      // $('#student_id').val(student_id);
      // $('#hidden_student_id').val(student_id);
      // getStudent(student_id,branch_id,session,status);
      // M.updateTextFields();  
    },
    error: function(obj, textStatus, errorThrown) {
      console.log("Error " + textStatus + ": " + errorThrown);
    }
  });
}

1 个答案:

答案 0 :(得分:0)

尝试一下

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($("#p1").html());
  });
});
</script>
</head>
<body>

   

<p id="p1">This is a paragraph.</p>

<button>Get text</button>

</body>
</html>