13

textラジオ ボタンに属するラベルを取得しようとしていますが、なかなか取得checkedできないようです。

$(document).ready(function(){
  $("body").on("click", "#btn", function(){

    var radios = $("input[type='radio']");

    $.each(radios, function(index, element){
      if ($(this).prop("checked") === true){

        var radioID = element.id;
        $("label").each(function(ind, elm){

          if ($(elm).prop("for") == radioID){
            console.log($(elm)); 
          }

        });
      }
    });
  });
});

jsbin

何らかの理由で印刷するだけです

-- [11:07:13.834] ({0:({}), context:({}), length:1}) @ http://jsbin.com/uniwum/2/edit:71

コンソールで。私は何を間違えましたか?

4

6 に答える 6

35

このようにしてください:-

$('#btn').click(function() {
    $("input[type='radio']:checked").each(function() {
        var idVal = $(this).attr("id");
        alert($("label[for='"+idVal+"']").text());
    });
});

ライブデモを参照

更新しました:

以下はドキュメントの参照です:-

于 2013-02-28T17:29:53.827 に答える
4

これを試して:

$('#btn').click(function(){
    $('table input[type="radio"]').each(function(){
        if($(this).is(':checked')){
        console.log($(this).parent().next().find('label').text());
        }
    });
});

jsFiddle の例

更新:ラジオ ボタンであるため、さらに高速な方法は次のとおりです。

$('#btn').click(function(){
    console.log( $('table input[type="radio"]:checked').parent().next().find('label').text() );
});

jsFiddle の例

于 2013-02-28T17:14:54.580 に答える
3
$("input[type='radio']:checked").parent().text()
于 2018-02-09T18:01:07.337 に答える
0

.text()次のように、要素を選択したら、要素のテキストを取得する関数を呼び出す必要があります$(elm).text()

この場合、elmはすでに DOM ノードであるため、plainelm.innerHTMLを呼び出して同じ結果を得ることができます。

于 2013-02-28T17:13:53.703 に答える
0

このコードを試してください:

$("td",$(":checked").parent().parent()).text()

jsFiddle へのリンクhttp://jsfiddle.net/rQX7H/2/

于 2013-02-28T17:14:06.603 に答える
0

ラジオ ボタン用に記述された HTML とラジオ ボタン用のテキストに応じて、さまざまな方法でこれを行うことができます。

方法 1 : 表示されるテキストと同じテキストを持つカスタム データ属性をラジオ ボタンに割り当てます。

    <input id="radio4" name="radios1" type="radio" value="4" data-value="radio 4" /> radio 4
    <input id="radio5" name="radios1" type="radio" value="4" data-value="radio 5"/> radio 5
    <input id="radio6" name="radios1" type="radio" value="4" data-value="radio 6"/> radio 6
    <input type="button" id="btn" value="Get from data-value" />

カスタム データ属性の詳細については、カスタム データ- * 属性を参照してください。

テキストを取得するためのスクリプト:

$(document).on("click", "#btn", function () {
            var value = $("input[name=radios1]:checked").attr("data-value");
            alert(value);
        });

方法 2 : すべてのラジオ ボタンに同じ [name] プロパティを割り当てるようにラジオ ボタンの html を変換し、ラジオ ボタンの「ラベル」フィールドを次のように配置します。

<input id="radio1" name="radios" type="radio"/>
        <label for="radio1"> radio 1</label>
    <input id="radio2" name="radios" type="radio"/>
        <label for="radio2"> radio 2</label>
    <input id="radio3" name="radios" type="radio"/>
        <label for="radio3"> radio 3</label>
    <input type="button" id="btn1" value="Get from Label" />

テキストを取得するためのスクリプト:

$(document).on("click", "#btn1", function () {
        var idVal = $('input[name=radios]:checked').attr("id");
        $("label[for='"+idVal+"']").text();
        //If you have the radio button in HTML table structure write it as below
        $('input[name=radios]:checked').parent().find('label').text();
    }

お役に立てれば :)

于 2014-08-21T11:13:05.080 に答える