当用户将鼠标悬停在 JQuery 中单选按钮的标签上时显示工具提示

发布于 2024-12-11 10:16:13 字数 1089 浏览 0 评论 0原文

我的表单中有一些单选按钮:

<div id="data_id-block">
<dt id="data_id-label"><label class="required">Data</label></dt>
<dd id="data_id-element">
    <label for="data_id-1">
        <input type="radio" name="data_id" id="data_id-1" value="1" />test 1
    </label><br />
    <label for="data_id-2">
        <input type="radio" name="data_id" id="data_id-2" value="2" />test 2
    </label><br />
    <label for="data_id-4">
        <input type="radio" name="data_id" id="data_id-4" value="4" /> Test Data
    </label><br />
    <label for="data_id-5">
        <input type="radio" name="data_id" id="data_id-5" value="5" /> Second Test Data
    </label><br />
    <label for="data_id-6">
        <input type="radio" name="data_id" id="data_id-6" value="6" />Unassigned
    </label>
</dd>

我试图在用户将鼠标悬停在单选按钮的标签上时显示工具提示。我可以做到这一点,但我也想获取单选按钮的“value”属性中的任何内容。我的尝试导致仅返回单选按钮的“值”,无论将鼠标悬停在哪个单选按钮上。

感谢您的帮助。

I have some radio buttons in a form:

<div id="data_id-block">
<dt id="data_id-label"><label class="required">Data</label></dt>
<dd id="data_id-element">
    <label for="data_id-1">
        <input type="radio" name="data_id" id="data_id-1" value="1" />test 1
    </label><br />
    <label for="data_id-2">
        <input type="radio" name="data_id" id="data_id-2" value="2" />test 2
    </label><br />
    <label for="data_id-4">
        <input type="radio" name="data_id" id="data_id-4" value="4" /> Test Data
    </label><br />
    <label for="data_id-5">
        <input type="radio" name="data_id" id="data_id-5" value="5" /> Second Test Data
    </label><br />
    <label for="data_id-6">
        <input type="radio" name="data_id" id="data_id-6" value="6" />Unassigned
    </label>
</dd>

I'm trying to display a tooltip when a user hovers over the label of the radio button. I can do this but I also want to get whatever is in the 'value' property of the radio button. My attempts to this resulted in only the 'value' of the radio button being returned regardless of which radio button was hovered over.

Appreciate the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-12-18 10:16:13
//this is just one way to register to the window.onLoad event in jQuery, i prefer it's readability for what is going on    
$(document).ready(function(){
//$("something", "something else") - 'Something Else' limits where jQuery searches for 'something'
// .hover( function A , function B ) - function A is the mouseover event, function B is the mouseleave event
// function(event){}  for All events, jQuery will always pass an "event" parameter if you ask for it on the window .event() registration.  cool stuff you can do with jQuery's "event" parameter can be found here in the references below.
      $("label", "#data_id-element").hover(function(event){
// $(this)  jQuery'itize the html Element the user hovered into 
// .attr("for") - get the value of the 'for' attribute
// $("#" + whatever) - get whatever this is by its id='' attribute
// .val()  - get the value of whatever this is
         var radioValue = $("#"+$(this).attr("for")).val();
      }, 
      function(event){
// $(this)  jQuery'itize the html Element the user hovered out of
// .attr("for") - get the value of the 'for' attribute
// $("#" + whatever) - get whatever this is by its id='' attribute
// .val()  - get the value of whatever this is
        var radioValue = $("#"+$(this).attr("for")).val();
      });

    });
//this is just one way to register to the window.onLoad event in jQuery, i prefer it's readability for what is going on    
$(document).ready(function(){
//$("something", "something else") - 'Something Else' limits where jQuery searches for 'something'
// .hover( function A , function B ) - function A is the mouseover event, function B is the mouseleave event
// function(event){}  for All events, jQuery will always pass an "event" parameter if you ask for it on the window .event() registration.  cool stuff you can do with jQuery's "event" parameter can be found here in the references below.
      $("label", "#data_id-element").hover(function(event){
// $(this)  jQuery'itize the html Element the user hovered into 
// .attr("for") - get the value of the 'for' attribute
// $("#" + whatever) - get whatever this is by its id='' attribute
// .val()  - get the value of whatever this is
         var radioValue = $("#"+$(this).attr("for")).val();
      }, 
      function(event){
// $(this)  jQuery'itize the html Element the user hovered out of
// .attr("for") - get the value of the 'for' attribute
// $("#" + whatever) - get whatever this is by its id='' attribute
// .val()  - get the value of whatever this is
        var radioValue = $("#"+$(this).attr("for")).val();
      });

    });
雄赳赳气昂昂 2024-12-18 10:16:13

如果我理解正确的话,当您将鼠标悬停在label 上时,您希望获取与label 关联的单选按钮的值。如果是这种情况,请尝试如下操作:

$("label").mouseover(function() {
    var radioButtonValue = $(this).find("input").val(); 
});

这是一个工作示例

If I've understood you correctly, you want to get the value of the radio button associated with a label, when you hover over the label. If that's the case, try something like this:

$("label").mouseover(function() {
    var radioButtonValue = $(this).find("input").val(); 
});

Here's a working example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文