0

这是软件生成的代码

<div id="cred_form_3584_1_wpcf-ticket-month-or-course-radios" class="myzebra-radios">
  <div class="myzebra-radios-single">
    <label class="myzebra-style-label">
      <input id="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-a6da15467aeb84f539c0dc1cd766ccd6-2" class="myzebra-control myzebra-radio myzebra-prime-name-wpcf-ticket-month-or-course" type="radio" checked="checked" value="wpcf-fields-radio-option-a6da15467aeb84f539c0dc1cd766ccd6-2" name="wpcf-ticket-month-or-course">
      <span class="myzebra-radio-replace"></span>
    </label>
    <label id="cred_form_3584_1_label_wpcf-ticket-month-or-course_wpcf-fields-radio-option-a6da15467aeb84f539c0dc1cd766ccd6-2" for="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-a6da15467aeb84f539c0dc1cd766ccd6-2">per month</label>
  </div>
<div class="myzebra-radios-single">
  <label class="myzebra-style-label">
    <input id="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" class="myzebra-control myzebra-radio myzebra-prime-name-wpcf-ticket-month-or-course" type="radio" value="wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" name="wpcf-ticket-month-or-course">
    <span class="myzebra-radio-replace"></span>
  </label>
  <label id="cred_form_3584_1_label_wpcf-ticket-month-or-course_wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" for="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1">per course</label>
</div>

使用此代码:

alert($('input[@name="myzebra-radios-single"]:checked').text());

我得到了价值,但没有得到文字。我如何获取文本(例如每月)?

4

3 回答 3

3

要获取 的内容label,您需要使用它的for=属性:

var $input = $('input[name=something]:checked');
var text = $('label[for='+$input.attr('id')+']').text();

http://jsfiddle.net/mblase75/RDPP7/

于 2013-01-14T14:47:30.787 回答
0

首先,单选元素没有任何文本值,如果有的话。

alert($('input[@name="myzebra-radios-single"]:checked').text());
// Should be
alert($('input[@name="wpcf-ticket-month-or-course"]:checked').text());

当我分解您的输入时,它们看起来像这样

<input 
    id="cred_form_3584_1_wpcf-ticket-month-or-course_wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" 
    class="myzebra-control myzebra-radio myzebra-prime-name-wpcf-ticket-month-or-course" 
    type="radio" 
    value="wpcf-fields-radio-option-f63eb739e2a6499a882c8e82aa35b028-1" 
    name="wpcf-ticket-month-or-course">

如果您想从上面选择某个属性,请使用 .val() .text() 或 .attr()。

于 2013-01-14T14:47:01.480 回答
0

尝试 :

alert($('input[@name="myzebra-radios-single"]:checked').attr('name'));

于 2013-01-14T14:48:11.203 回答