繁体   English   中英

如何在 div 的单选按钮中显示 label 文本

[英]How to display the label text in a radio button in a div

使用 jQuery 或 JavaScript 如何在标签内显示文本。 即文本是5和6

<label class="radio">
    <label style="display: none;">
        <input type="radio" name="x_keeper1_price" id="x_keeper1_price_0" value="21"/>
    </label>
    5
</label>

<label class="radio">
    <label style="display: none;">
        <label style="display: none;">
            <input type="radio" name="x_Defd5_price" id="x_Defd5_price_0" value="28"/>
        </label>
    </label>
    6
</label>


<div id="Price" class="Price">
   display 5 here 
</div>
<div id="Price2" class="Price2">
    display 6 here 
</div>

你的 HTML 代码有点奇怪,嵌套label标签不是一个好主意,我已经准备了一个 JSFiddle 和我的建议: http2//jsfiddleRg//jsfiddleRg//

修改后的 HTML 代码:

<label id="l1" class="radio"><span style="display: none;">
    <input type="radio" name="x_keeper1_price" id="x_keeper1_price_0" value="21"/>
</span>5</label>

<label id="l2" class="radio"><label style="display: none;">
    <span style="display: none;"><input type="radio" name="x_Defd5_price" id="x_Defd5_price_0" value="28"/>
</span></label>6</label>

<div id="price" class="Price">
   display 5 here 
</div>
<div id="price2" class="Price2">
    display 6 here 
</div>

关于 jQuery 代码,非常简单,可以使用text()方法读取和设置标签中的文本内容,例如:

$('#price').text($('#l1').text());
$('#price2').text($('#l2').text());

text()方法描述:获取匹配元素集中每个元素的组合文本内容,包括它们的后代。

首先你必须让你的结构正确。 您的 HTML 需要简单

基本 label 声明:

<input type="radio" name="x_keeper1_price" id="x_keeper1_price_0" value="21">
<label for="x_keeper1_price_0">5</label>

然后,如果您希望 label 显示文本显示在单独的 div 中,请执行以下操作:

//get the text from the lable
var lableText = $("label[for='x_keeper1_price_0']").text();

//put it inside the div
$("#Price").html("display " + lableText + " here");

你的问题有点令人困惑,所以我希望这就是你要找的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM