jquery find class and get the value

前端 未结 5 1094
终归单人心
终归单人心 2020-12-13 12:38

I am trying to get the value of an input text field.

the HTML is:

相关标签:
5条回答
  • 2020-12-13 12:56

    Class selectors are prefixed with a dot. Your .find() is missing that so jQuery thinks you're looking for <myClass> elements.

    var myVar = $("#start").find('.myClass').val();
    
    0 讨论(0)
  • 2020-12-13 12:56

    var myVar = $("#start").find('myClass').val();

    needs to be

    var myVar = $("#start").find('.myClass').val();

    Remember the CSS selector rules require "." if selecting by class name. The absence of "." is interpreted to mean searching for <myclass></myclass>.

    0 讨论(0)
  • 2020-12-13 13:04

    You can get value of id,name or value in this way. class name my_class

     var id_value = $('.my_class').$(this).attr('id'); //get id value
     var name_value = $('.my_class').$(this).attr('name'); //get name value
     var value = $('.my_class').$(this).attr('value'); //get value any input or tag
    
    0 讨论(0)
  • 2020-12-13 13:11
    var myVar = $("#start").find('.myClass').first().val();
    
    0 讨论(0)
  • 2020-12-13 13:12

    You can also get the value by the following way

    $(document).ready(function(){
      $("#start").click(function(){
        alert($(this).find("input[class='myClass']").val());
      });
    });
    
    0 讨论(0)
提交回复
热议问题