从表单的select选项的值中获取jquery数据属性

从表单的select选项的值中获取jquery数据属性,第1张

概述我试图从下面的表单中的选项中获取数据的值,所以我可以在jQuery中使用它们,但我似乎似乎没有成功.我想做的很简单,你会在下面的代码中看到.我怎么能这样做? $('#selectForm').click(function(e) { e.preventDefault(); var action = $(this).data('id'); var ty 我试图从下面的表单中的选项中获取数据的值,所以我可以在jquery中使用它们,但我似乎似乎没有成功.我想做的很简单,你会在下面的代码中看到.我怎么能这样做? @H_403_7@

@H_403_7@

$('#selectForm').click(function(e) {        e.preventDefault();        var action = $(this).data('ID');        var type = $(this).data('name');        console.log(action+' '+type);    });
@H_403_7@// jquery的

@H_403_7@

<form ID="selecttest">    <label for="fruit">Please select at least two fruits</label><br>        <select ID="fruit" name="fruit" size="5">            <option data-ID="1" data-name="norman">Banana</option>            <option data-ID="2" data-name="james">Apple</option>            <option data-ID="3" data-name="lars">Peach</option>            <option data-ID="4" data-name="john">Turtle</option>        </select>        <input type="submit" ID="selectForm" value="ValIDate Selecttests">    </form>
解决方法 这是演示 – http://jsfiddle.net/tnVfV/ @H_403_7@

@H_403_7@这是代码:

@H_403_7@

$('#selectForm').click(function (e) {    e.preventDefault();    var action = $('#fruit option:selected').attr('data-ID');    var type = $('#fruit option:selected').attr('data-name');    console.log(action + ' ' + type);});
@H_403_7@data-ID和data-name是选项的属性.您可以使用本机属性ID和名称…

@H_403_7@好的,因为tymeJV评论这里是使用data()的代码:

@H_403_7@

$('#selectForm').click(function (e) {    e.preventDefault();    var action = $('#fruit option:selected').data('ID');    var type = $('#fruit option:selected').data('name');    console.log(action + ' ' + type);});
@H_403_7@演示http://jsfiddle.net/tnVfV/1/

总结

以上是内存溢出为你收集整理的从表单的select选项的值中获取jquery数据属性全部内容,希望文章能够帮你解决从表单的select选项的值中获取jquery数据属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1052278.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存