JQuery为选择的无线电获取标签值

时间:2017-08-24 17:03:34

标签: javascript jquery

我有一些看起来像这样的HTML:

sortDescriptors

我想(从函数中)获取已选中的单选框的标签值。我认为它看起来像这样:

 <label>
<input type="radio" onchange="routesearch2()" name="LargestMeeting" value="0" id="LargestMeeting_0" />
Less Than 5,000 sqft (<span id="lm1" class="detect lm1"></span>)</label>
 <br />
  <label>
<input type="radio" onchange="routesearch2()" name="LargestMeeting" value="5000" id="LargestMeeting_1" />
5,000 or more sqft (<span id="lm2" class="detect lm2"></span>)</label>
  <br />
  <label>
   <input type="radio" onchange="routesearch2()" name="LargestMeeting" value="10000" id="LargestMeeting_2" />
10,000 or more sqft (<span id="lm3" class="detect lm3"></span>)</label>
  <br />
<label>
<input type="radio" onchange="routesearch2()" name="LargestMeeting" value="20000" id="LargestMeeting_3" />
20,000 or more sqft (<span id="lm4" class="detect lm4"></span>)</label>
<br />
<label>
<input type="radio" onchange="routesearch2()" name="LargestMeeting" value="30000" id="LargestMeeting_4" />
   30,000 or more sqft (<span id="lm5" class="detect lm5"></span>)</label>

我错过了什么?它空白了。

4 个答案:

答案 0 :(得分:1)

使用此

$("input[name=LargestMeeting]:checked").parent().text()

并清理空白区域,您可以添加

$.trim($("input[name=LargestMeeting]:checked").parent().text())

答案 1 :(得分:0)

由于input标记是自动关闭的,因此您无法获得所需的文字。您需要先选择已检查的输入,然后选择父级,然后按text()获取内容并调用trim()来修剪文本。

var mylabel = $("label input[name=LargestMeeting]:checked").parent().text().trim();

&#13;
&#13;
$('input[type=radio]').on('click', function() {
  var mylabel = $("label input[name=LargestMeeting]:checked").parent().text().trim();
  console.log(mylabel);
});


//place holder
function routesearch2() {};
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  <input type="radio" onchange="routesearch2()" name="LargestMeeting" value="0" id="LargestMeeting_0" /> Less Than 5,000 sqft (<span id="lm1" class="detect lm1"></span>)</label>
<br />
<label>
  <input type="radio" onchange="routesearch2()" name="LargestMeeting" value="5000" id="LargestMeeting_1" /> 5,000 or more sqft (<span id="lm2" class="detect lm2"></span>)</label>
<br />
<label>
  <input type="radio" onchange="routesearch2()" name="LargestMeeting" value="10000" id="LargestMeeting_2" /> 10,000 or more sqft (<span id="lm3" class="detect lm3"></span>)</label>
<br />
<label>
  <input type="radio" onchange="routesearch2()" name="LargestMeeting" value="20000" id="LargestMeeting_3" /> 20,000 or more sqft (<span id="lm4" class="detect lm4"></span>)</label>
<br />
<label>
  <input type="radio" onchange="routesearch2()" name="LargestMeeting" value="30000" id="LargestMeeting_4" /> 30,000 or more sqft (<span id="lm5" class="detect lm5"></span>)</label>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用Javascript

var meetings = document.getElementsByName('LargestMeeting');
for(var i = 0; i < meetings.length; i++){
    if(meetings[i].checked){
        selected_value = meetings[i].value;
    }
}

答案 3 :(得分:0)

你非常接近。问题在于嵌套选择器的方式。如果您抓住选中的input并选择其父级,则可以轻松输出所需的文本。

见这个例子:

&#13;
&#13;
function routesearch2() {
  var myLabel = $('input[name=LargestMeeting]:checked').parent().text();
  alert(myLabel);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="radio" onchange="routesearch2()" name="LargestMeeting" value="0" id="LargestMeeting_0" />
Less Than 5,000 sqft (<span id="lm1" class="detect lm1"></span>)</label>
<br />
<label>
<input type="radio" onchange="routesearch2()" name="LargestMeeting" value="5000" id="LargestMeeting_1" />
5,000 or more sqft (<span id="lm2" class="detect lm2"></span>)</label>
<br />
<label>
   <input type="radio" onchange="routesearch2()" name="LargestMeeting" value="10000" id="LargestMeeting_2" />
10,000 or more sqft (<span id="lm3" class="detect lm3"></span>)</label>
<br />
<label>
<input type="radio" onchange="routesearch2()" name="LargestMeeting" value="20000" id="LargestMeeting_3" />
20,000 or more sqft (<span id="lm4" class="detect lm4"></span>)</label>
<br />
<label>
<input type="radio" onchange="routesearch2()" name="LargestMeeting" value="30000" id="LargestMeeting_4" />
   30,000 or more sqft (<span id="lm5" class="detect lm5"></span>)</label>
&#13;
&#13;
&#13;