如何在jQuery中获取元素值

时间:2021-01-20 18:57:22

Why is it not returning value in 'li'? What am i doing wrong?

为什么它不会在'li'中返回值?我究竟做错了什么?

$("#list li").click(function() {
    var selected = $(this).val();
    alert(selected);
})

9 个解决方案

#1


55  

Did you want the HTML or text that is inside the li tag?

您想要li标签内的HTML或文本吗?

If so, use either:

如果是这样,请使用:

$(this).html()

or:

要么:

$(this).text()

The val() is for form fields only.

val()仅用于表单字段。

#2


6  

<ul id="unOrderedList">
<li value="2">Whatever</li>
.
.



 $('#unOrderedList li').click(function(){
      var value = $(this).attr('value');
     alert(value); 
  });

Your looking for the attribute "value" inside the "li" tag

您在“li”标记内查找属性“value”

#3


3  

Use .text() or .html()

使用.text()或.html()

$("#list li").click(function() {
        var selected = $(this).text();
        alert(selected);
});

#4


2  

A li doesn't have a value. Only form-related elements such as input, textarea and select have values.

李没有价值。只有与表单相关的元素(如input,textarea和select)才具有值。

#5


2  

Most probably, you want something like this:

最有可能的是,你想要这样的东西:

$("#list li").click(function() {
        var selected = $(this).html();
        alert(selected);
});

#6


1  

  <div class="inter">
      <p>Liste des Produits</p>
      <ul>
          <li><a href="#1">P1</a></li>
          <li><a href="#2">P2</a></li>
          <li><a href="#3">P3</a></li>
      </ul>
  </div>

        $(document).ready(function(){
            $(".inter li").bind(
            "click", function(){
                alert($(this).children("a").text());
            });
        });

#7


1  

You can do the same by using jQuery on().

你可以在()上使用jQuery来做同样的事情。

$("#list").on('click','li',(function() {
    var selected = $(this).text();   //or .html()
    alert(selected);
})

#8


0  

$("#list li").click(function() {
        var selected = $(this).html();
        alert(selected);
});

#9


0  

To get value of li we should use $("#list li").click(function() { var selected = $(this).html();

要获得li的值,我们应该使用$(“#list li”)。click(function(){var selected = $(this).html();

or

要么

var selected = $(this).text();

alert(selected);

})

})

#1


55  

Did you want the HTML or text that is inside the li tag?

您想要li标签内的HTML或文本吗?

If so, use either:

如果是这样,请使用:

$(this).html()

or:

要么:

$(this).text()

The val() is for form fields only.

val()仅用于表单字段。

#2


6  

<ul id="unOrderedList">
<li value="2">Whatever</li>
.
.



 $('#unOrderedList li').click(function(){
      var value = $(this).attr('value');
     alert(value); 
  });

Your looking for the attribute "value" inside the "li" tag

您在“li”标记内查找属性“value”

#3


3  

Use .text() or .html()

使用.text()或.html()

$("#list li").click(function() {
        var selected = $(this).text();
        alert(selected);
});

#4


2  

A li doesn't have a value. Only form-related elements such as input, textarea and select have values.

李没有价值。只有与表单相关的元素(如input,textarea和select)才具有值。

#5


2  

Most probably, you want something like this:

最有可能的是,你想要这样的东西:

$("#list li").click(function() {
        var selected = $(this).html();
        alert(selected);
});

#6


1  

  <div class="inter">
      <p>Liste des Produits</p>
      <ul>
          <li><a href="#1">P1</a></li>
          <li><a href="#2">P2</a></li>
          <li><a href="#3">P3</a></li>
      </ul>
  </div>

        $(document).ready(function(){
            $(".inter li").bind(
            "click", function(){
                alert($(this).children("a").text());
            });
        });

#7


1  

You can do the same by using jQuery on().

你可以在()上使用jQuery来做同样的事情。

$("#list").on('click','li',(function() {
    var selected = $(this).text();   //or .html()
    alert(selected);
})

#8


0  

$("#list li").click(function() {
        var selected = $(this).html();
        alert(selected);
});

#9


0  

To get value of li we should use $("#list li").click(function() { var selected = $(this).html();

要获得li的值,我们应该使用$(“#list li”)。click(function(){var selected = $(this).html();

or

要么

var selected = $(this).text();

alert(selected);

})

})