获取特定li的复选框值

时间:2016-07-04 10:08:51

标签: javascript jquery

我有以下html结构。我可以选择<li>,但无法获取复选框值 - 它是undefined

<ul>
    <li class="selected" style="false">
        <label class="">
            <input type="checkbox" data-name="selectItem" value="2447"> 
            <span>England</span>
        </label>
    </li>
</ul>
$('ul li.selected').each(function () {
    console.log('li.selected');
    var val = $(this).closest('label').find('[type=checkbox]').val();
    console.log(val);                                        
});

5 个答案:

答案 0 :(得分:1)

feedNoteLabel.numberOfLines = 0 feedNoteLabel.sizeToFit() feedNoteLabel.layoutIfNeeded() 上升到DOM,你应该使用closest()来代替你。另请注意,您只需拨打一次find()即可达到所需要的效果。试试这个:

find()

答案 1 :(得分:0)

每个$(this)里面都是li元素,所以试试这个:

import java.util.HashMap;

 public class Test {
   public static void main(String[] args) {
     HashMap hm = new HashMap();

     String key = "k1";
     hm.put(key, "1");
     hm.put(key, "2");
   }
} 

答案 2 :(得分:0)

尝试更改您的代码:

&#13;
&#13;
/************************************************************
* Interrupt Vectors (offset from 0xFF80)
************************************************************/

#define RTC_VECTOR          (0x0052) /* 0xFFD2 RTC */
#define PORT2_VECTOR        (0x0054) /* 0xFFD4 Port 2 */
#define USCI_B3_VECTOR      (0x0056) /* 0xFFD6 USCI B3 Receive/Transmit */
#define USCI_A3_VECTOR      (0x0058) /* 0xFFD8 USCI A3 Receive/Transmit */
#define USCI_B1_VECTOR      (0x005A) /* 0xFFDA USCI B1 Receive/Transmit */
#define USCI_A1_VECTOR      (0x005C) /* 0xFFDC USCI A1 Receive/Transmit */
#define PORT1_VECTOR        (0x005E) /* 0xFFDE Port 1 */
#define TIMER1_A1_VECTOR    (0x0060) /* 0xFFE0 Timer1_A3 CC1-2, TA1 */
#define TIMER1_A0_VECTOR    (0x0062) /* 0xFFE2 Timer1_A3 CC0 */
#define DMA_VECTOR          (0x0064) /* 0xFFE4 DMA */
#define USCI_B2_VECTOR      (0x0066) /* 0xFFE6 USCI B2 Receive/Transmit */
#define USCI_A2_VECTOR      (0x0068) /* 0xFFE8 USCI A2 Receive/Transmit */
#define TIMER0_A1_VECTOR    (0x006A) /* 0xFFEA Timer0_A5 CC1-4, TA */
#define TIMER0_A0_VECTOR    (0x006C) /* 0xFFEC Timer0_A5 CC0 */
#define ADC12_VECTOR        (0x006E) /* 0xFFEE ADC */
#define USCI_B0_VECTOR      (0x0070) /* 0xFFF0 USCI B0 Receive/Transmit */
#define USCI_A0_VECTOR      (0x0072) /* 0xFFF2 USCI A0 Receive/Transmit */
#define WDT_VECTOR          (0x0074) /* 0xFFF4 Watchdog Timer */
#define TIMER0_B1_VECTOR    (0x0076) /* 0xFFF6 Timer0_B7 CC1-6, TB */
#define TIMER0_B0_VECTOR    (0x0078) /* 0xFFF8 Timer0_B7 CC0 */
#define UNMI_VECTOR         (0x007A) /* 0xFFFA User Non-maskable */
#define SYSNMI_VECTOR       (0x007C) /* 0xFFFC System Non-maskable */
#define RESET_VECTOR        (0x007E) /* 0xFFFE Reset [Highest Priority] */
&#13;
 $('li.selected').each(function () {
            console.log("li.selected");
            var val = $(this).find("label").find('input[type=checkbox]').val();
            console.log(val);                                        
        });
&#13;
&#13;
&#13;

答案 3 :(得分:0)

只需搜索复选框:val = $(this).find('input[type=checkbox]').val();

$('ul li.selected').each(function() {
  console.log("li.selected");
  var val = $(this).find('input[type=checkbox]').val();
  console.log(val);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li class="selected" style="false">
    <label class="">
      <input type="checkbox" data-name="selectItem" value="2447">
      <span>England</span>
    </label>
  </li>
</ul>

答案 4 :(得分:0)

您可以直接.find() $(this)中的input type="checkbox"

&#13;
&#13;
$('ul li.selected').each(function () {
  console.log('li.selected');
  var val = $(this).find(':checkbox').val();
  console.log(val);                                        
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
    <li class="selected" style="false">
        <label class="">
              <input type="checkbox" data-name="selectItem" value="2447"> 
              <span>England</span>
        </label>
    </li>
</ul>
&#13;
&#13;
&#13;