zoukankan      html  css  js  c++  java
  • zepto区别于jquery获取select表单选中的值

    在jquery下,我们获取select表单选中的值通常是通过$('select').val()来实现,这样的方式简单又明了,或者通过$('select option[selected]').text()$('select option:selected').text()这样的代码来实现,其实本来$('select').val()这种方式就很好,但在项目中我想获取给每个option自定义的属性的值,我就要通过后者的类似的写法$('select option:selected').attr('data-id')来实现,但是这种方法在zepto下就会报出"Failed to execute 'querySelectorAll' on 'Document': 'select option:selected' is not a valid selector."的错误,这让我曾经一度认为是我的写法有错误,后来通过网上查资料,大神们才让我知道了这种写法不存在在zepto的API里,他们给出了这样的解决办法:
    办法一:

    $('select option').not(function(){ return !this.selected }).attr('data-id');
    

    办法一适用于jquery和zepto。

    办法二:

    $('select  option').eq($('select').attr("selectedIndex")).attr('data-id');
    

    办法二只适用于zepto,在jquery下会报出undefined,所以不适用于jquery。

  • 相关阅读:
    问题汇总
    Spring boot开发过程遇到的一些小问题
    Java 7 新特性
    I2C总线协议详解
    画布分割算法
    nordic __noinit__变量使用
    RTOS事件组使用流程
    RTOS软件定时器的使用
    RTOS互斥信号量的使用流程
    RTOS优先级翻转
  • 原文地址:https://www.cnblogs.com/tnnyang/p/5915633.html
Copyright © 2011-2022 走看看