简体   繁体   English

如何从带有标签的单选按钮中提取值?

[英]How do I pull values from a radio button with a label?

In regards to question getting the text of radio button instead of values , How do I pull values from a radio button with a label?关于获取单选按钮文本而不是值的问题,如何从带有标签的单选按钮中提取值?

I did not post this question there, I fealt it did not go with the original question.我没有在那里发布这个问题,我觉得它与原始问题不符。

How would you then get the value of the selected radio?那么你将如何获得所选收音机的价值? I have tried both $("input[type='radio'][id='rblUnitOfMeasureType']:checked").val() and $("input[type='radio'][id='rblUnitOfMeasureType']:checked + label").val() , they both return undefined.我已经尝试过$("input[type='radio'][id='rblUnitOfMeasureType']:checked").val()$("input[type='radio'][id='rblUnitOfMeasureType']:checked + label").val() ,它们都返回未定义。

I am using a form, but the form is just there as a from tag, the rest of the html is basically a formless form (if that makes sense).我正在使用一个表单,但该表单只是作为 from 标签存在,其余的 html 基本上是一个无表单的表单(如果有道理的话)。

 function listUnitOfMeasureSet_change() { if (listUnitOfMeasureSet.value.toString().toLowerCase() === "new") { $("#divNewTypeUnitContentHolder").html(""); callService("GET", g_WebServiceUnitsOfMeasureTypeUnitsURL, null, function(jsonResult) { if (jsonResult.Success) { for (i = 0; i < jsonResult.UnitOfMeasureTypeList.length; i++) { $("#divNewTypeUnitContentHolder").html($("#divNewTypeUnitContentHolder").html() + '<input type="radio" id="rblUnitOfMeasureType' + i + '" name="rblUnitOfMeasureType" value="' + jsonResult.UnitOfMeasureTypeList[i].ID + '" /><label for="rblUnitOfMeasureType' + i + '">' + jsonResult.UnitOfMeasureTypeList[i].Name + '</label> ' + jsonResult.UnitOfMeasureTypeList[i].Description + '<br />'); } } }); $("#divNewUnitOfMeasure").dialog("open"); } }

The first one should work, another option is to use .attr('value')第一个应该工作,另一个选择是使用.attr('value')

 $('input').click(function(){ console.log($("input[type='radio'][id*='rblUnitOfMeasureType']:checked").val()); console.log($("input[type='radio'][id*='rblUnitOfMeasureType']:checked").attr('value')); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type='radio' id='rblUnitOfMeasureType' value='15'> <input type='radio' id='rblUnitOfMeasureType4' value='17'>

if you check for the one that is checked first and then if you use that selector with:如果您检查首先检查的那个,然后如果您使用该选择器:

("#input_test").attr('text')

it returns the text inside the radio input.它返回收音机输入中的文本。

On radio select trigger a function and capture it's detail as below:radio选择上触发一个功能并捕获它的详细信息如下:

$("input:radio[name=radios]").click(function() {
    alert($(this).val());
    alert($(this).parent().next().find('label').text());
});

Reference html:参考html:

<table>
    <tr>
      <td>
        <input id="radio1" name="radios" type="radio" value="radio 1"/>
      </td>
      <td>
        <label for="radio1">
          check radio 1
        </label>
      </td>
    </tr>
    <tr>
      <td>
          <input id="radio2" name="radios" type="radio" value="radio 2"/>
      </td>
      <td>
        <label for="radio2">
          check radio 2
        </label>
      </td>
    </tr>
  </table>

jsfiddle for the same. jsfiddle相同。

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

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