【问题标题】:detect selected list with specific value检测具有特定值的选定列表
【发布时间】:2011-10-10 10:49:15
【问题描述】:

我想在我的页面上检测用户未更改其值的下拉列表。 为了这个目标,我正在尝试搜索仍然选择默认值的所有下拉列表。

var t =  $("select[value='Select Team'] select:selected").attr("id");
alert(t);

警报是:'未定义'

HTML 代码:

 <TMPL_LOOP DATA_ROLES>
        <tr id="<TMPL_VAR ID>">
            <td><select class="role2" id="role2_<TMPL_VAR ID>" name="role2_<TMPL_VAR ID>" title="The role of the employee"><TMPL_VAR ROLE></select></td>
            <td><input class="steps" id="steps_<TMPL_VAR ID>" type="text" name="steps_<TMPL_VAR ID>" size="5" title="Total amount of steps per month" value="<TMPL_VAR STEP>"></td>
            <td><input class="measurable_steps" id="measurable_steps_<TMPL_VAR ID>" type="text" name="measurable_steps_<TMPL_VAR ID>" title="Measurable steps" value="<TMPL_VAR MEASURABLE_STEP>"></td>
            <td><input class="steps_ratio" id="steps_ratio_<TMPL_VAR ID>" type="text" name="steps_ratio_<TMPL_VAR ID>" title="'Measurable steps' / 'Steps'" value="<TMPL_VAR STEPS_RATIO>%" readonly></td>
            <td style="text-align: center"><select class="reopen_rate" id="reopen_rate_<TMPL_VAR ID>" name="reopen_rate_<TMPL_VAR ID>" title="How many reopenes after closing the SI"><TMPL_VAR REOPEN></select></td>
            <td><select class="w4p" id="w4p_<TMPL_VAR ID>" name="w4p_<TMPL_VAR ID>"><TMPL_VAR W4P></select></td>
            <td><select class="team" id="team_<TMPL_VAR ID>" name="team2_<TMPL_VAR ID>"><TMPL_VAR TEAM></select></td>
            <td><input class="checkbox" id="checkbox_<TMPL_VAR ID>" type=checkbox></td>
            <td><input class="status2" id="status2_<TMPL_VAR ID>" type="hidden" name="status2_<TMPL_VAR ID>" value="<TMPL_VAR STATUS>"></td>
        </tr>
</TMPL_LOOP>

你能帮忙吗?

谢谢!

【问题讨论】:

  • 请你把你的select标签的html放上去
  • 你试过select[value='Select Team']:selected
  • 您阅读我的回答有帮助吗?
  • 不起作用 - 添加了 html...
  • 我的情况如下:linkjsfiddle.net/BFMD4/3

标签: jquery select attr


【解决方案1】:
<select value='Select Team' id='select'>
    <option id='1'>1</option>
    <option id='2'>2</option>
    <option id='3' selected >3</option>
    <option id='4'>4</option>
</select>


var t =  $('option[value='+$("#select").val()+']').attr("id");
alert(t);

你也可以这样

var t =  $("select option:selected").attr("id");
alert(t);

你可以在这里试试http://jsfiddle.net/BFMD4/1/

【讨论】:

  • 它不起作用:var t = $('option[value='+$(".team").val()+']').attr("id"); - 添加了 html 代码...
【解决方案2】:

Option 对象具有 defaultSelected 属性,如果最初选择了该选项(如您的情况),则该属性为 true。您只需检查所选选项是否同时为defaultSelected

var $notChanged = $('select').filter(function () {
        return $(this).find('option:selected').get(0).defaultSelected
    });

// $notChanged is populated with selects that are not changed

您的情况:http://jsfiddle.net/BFMD4/4/

也就是说,如果您想查找自页面加载后未更改的内容。

但如果您想测试特定值,您还有其他选择。

第一:如果你可以在你的option元素中使用values

<select>
    <option>Please select one...</option>
    <option value="apples">Apples</option>
    <option value="oranges">Oranges</option>
    <option value="ponies">Ponies</option>
</select>

然后需要检查select是否有空值以外的值。

var $notChanged = $('select').filter(function () {
            return $(this).val() === ''
        });
// the $notChanged collection contains unchanged select lists

第二:如果你绝对必须,就像你的情况一样,使用 id 来存储值,而不是 value 属性

您可以将一个类(例如“initial”)添加到您希望成为初始选项的选项中: 请选择一个... 苹果 橘子 小马

然后你像这样寻找这些:

var $notChanged = $('select').filter(function () {
        return $(this).find('option.initial:selected').length > 0
    });

【讨论】:

  • 它看起来不错,但对我没有帮助,因为如果初始值不是“选择团队”(即团队 1、团队 2、团队 3)那么对我来说没问题 - 我希望只找到案例其中初始值为“选择团队”(如果可能,不包含每个语句)。
猜你喜欢
  • 2022-01-17
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2020-02-27
  • 2011-01-21
  • 1970-01-01
相关资源
最近更新 更多