JQuery:包含完全匹配的功能限制 [英] JQuery :contains function limit to exact match

查看:22
本文介绍了JQuery:包含完全匹配的功能限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 JQuery :contains 函数来决定从 SESSION 变量中选择哪个选项.

Using the JQuery :contains function to decide which option on a select is selected from a SESSION variable.

contains 函数匹配了错误的选项,因为它们是一个选项PADI 开放水域"和另一个PADI 开放水域潜水教练"

The contains function is matching the wrong option as their is an option 'PADI Open Water' and another 'PADI Open Water Scuba Instructor'

我们如何限制它以匹配确切的内容而不是更多?

How do we limit it to match the exact content and no more?

$('option:contains("<?php echo $_SESSION['divelevel'];?>")').attr('selected', 'selected');

推荐答案

尝试使用 .filter(),找到你想要的选项.

Try using .filter(), to find the option you want.

$('option').filter(function(){
    return $(this).html() == "<?php echo $_SESSION['divelevel'];?>";
}).attr('selected', 'selected');

这篇关于JQuery:包含完全匹配的功能限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆