Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript attr(';value';)返回未定义的值_Javascript_Jquery_Html - Fatal编程技术网

Javascript attr(';value';)返回未定义的值

Javascript attr(';value';)返回未定义的值,javascript,jquery,html,Javascript,Jquery,Html,我试图得到一个5星级评级的值属性,但是当我把鼠标悬停在它上面时,我得到了未定义的值属性。当我使用attr('class')而不是attr('value')时,它是如何工作的。我想知道是否有人能帮我。谢谢 <fieldset class="rating"> <input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awes

我试图得到一个5星级评级的值属性,但是当我把鼠标悬停在它上面时,我得到了未定义的值属性。当我使用attr('class')而不是attr('value')时,它是如何工作的。我想知道是否有人能帮我。谢谢

 <fieldset class="rating">
    <input type="radio" id="star5" name="rating" value="5" /><label class = "full" for="star5" title="Awesome - 5 stars"></label>
    <input type="radio" id="star4half" name="rating" value="4 and a half" /><label class="half" for="star4half" title="Pretty good - 4.5 stars"></label>
    <input type="radio" id="star4" name="rating" value="4" /><label class = "full" for="star4" title="Pretty good - 4 stars"></label>
    <input type="radio" id="star3half" name="rating" value="3 and a half" /><label class="half" for="star3half" title="Meh - 3.5 stars"></label>
    <input type="radio" id="star3" name="rating" value="3" /><label class = "full" for="star3" title="Meh - 3 stars"></label>
    <input type="radio" id="star2half" name="rating" value="2 and a half" /><label class="half" for="star2half" title="Kinda bad - 2.5 stars"></label>
    <input type="radio" id="star2" name="rating" value="2" /><label class = "full" for="star2" title="Kinda bad - 2 stars"></label> 
    <input type="radio" id="star1half" name="rating" value="1 and a half" /><label class="half" for="star1half" title="Meh - 1.5 stars"></label>
    <input type="radio" id="star1" name="rating" value="1" /><label class = "full" for="star1" title="Sucks big time - 1 star"></label>
    <input type="radio" id="starhalf" name="rating" value="half" /><label class="half" for="starhalf" title="Sucks big time - 0.5 stars"></label>

</fieldset>

有人发布了解决方案,它成功了。但是由于某种原因它被删除了,所以我将发布解决方案

$('.rating').on('mouseover', 'label', function(e) {
   console.log($(this).prev().val());


});

有人发布了解决方案,它成功了。但是由于某种原因它被删除了,所以我将发布解决方案

$('.rating').on('mouseover', 'label', function(e) {
   console.log($(this).prev().val());


});

更改事件侦听器它当前正在警告字段集上的任何鼠标悬停事件,如果您只对输入标记div感兴趣,那么您需要
。分级输入
而不是
。分级

选项1:

 $(document).on('mouseover', '.rating input', function(e) {
       var c = $(e.target).attr('value')
       console.log(c);
       alert(c);
  });

选项2:

 $(document).on('mouseover', '.rating input', function(e) {
       var c = $(e.target).attr('value')
       console.log(c);
       alert(c);
  });
另一个选项是使用JQuery
.is
这允许您测试元素集的内容,并且可以将原始事件侦听器保留在
.rating
标记上

 $( ".rating" ).on('mouseover', function( event ) {  
  var target = $( event.target );  
  if ( target.is( "input" ) ) {  
      var value = target.attr('value')
      alert(value);
  }  
});  


希望对您有所帮助。

更改事件侦听器它当前正在提醒字段集上的任何鼠标悬停事件,如果您只对输入标记div感兴趣,那么您需要
。评级输入
而不是
。评级

选项1:

 $(document).on('mouseover', '.rating input', function(e) {
       var c = $(e.target).attr('value')
       console.log(c);
       alert(c);
  });

选项2:

 $(document).on('mouseover', '.rating input', function(e) {
       var c = $(e.target).attr('value')
       console.log(c);
       alert(c);
  });
另一个选项是使用JQuery
.is
这允许您测试元素集的内容,并且可以将原始事件侦听器保留在
.rating
标记上

 $( ".rating" ).on('mouseover', function( event ) {  
  var target = $( event.target );  
  if ( target.is( "input" ) ) {  
      var value = target.attr('value')
      alert(value);
  }  
});  


希望有帮助。

您将侦听器添加到了错误的元素。类别“评级”指的是字段集,而不是“输入”

一个可能的修正是在每个输入中添加“class=”rating“,如下所示:

$(document).on('mouseover','.rating',函数(e){
var c=$(e.target.attr('value'))
警报(c);
});

您将侦听器添加到了错误的元素。类别“评级”指的是字段集,而不是“输入”

一个可能的修正是在每个输入中添加“class=”rating“,如下所示:

$(document).on('mouseover','.rating',函数(e){
var c=$(e.target.attr('value'))
警报(c);
});


当我尝试那样做时,它不会发出任何警报。当我尝试那样做时,它不会发出任何警报