【问题标题】:jQuery return the first match in a select listjQuery 返回选择列表中的第一个匹配项
【发布时间】:2011-01-04 04:17:45
【问题描述】:

我想在选择列表中选择与某个值匹配的第一个选项,当前选择了最后一个匹配项,如下所示:

jQuery('.edit-item').click(function(){
   var id = jQuery(this).attr('id');
   var assignee = jQuery('#assignee-'+id).text();
   jQuery('#edit-form-'+id+' select[name=item[responsible]]').val(assignee); 
   return false;
});

<select name="item[responsible]">
  <option value="*">Anyone</option>
  <option value="'+$user+'">Me ('+$user+')</option>')
   -- here a loop with groups --
    <option value="">----------</option>
    <option value="'+$group+'">'+$group+'</option>')
    -- here a loop with all usernames within $group
    <option value="'+$username+'">'+$username+'</option>')
    -- end loop --
   -- end loop --
 </select>')

现在,如果最终用户是该用户在他所在的组中被选中的受让人,而不是第二个值“我(用户)”,有没有办法做到这一点,或者我需要一些解决方法吗?

在用例中,受让人是后端用户,这很简单

<select name="item[responsible]">
 <option value="*">Anyone</option>
  <option value="mister x" selected=selected>Me (mister x)</option> // this one needs to be selected
  <option value="1">1</option>
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="2">2</option>
  <option value="c">c</option>
  <option value="d">d</option>
  <option value="mister x">mister x</option> // this is the one that is selected
</select>

好的,所以selected=selected而不是checked=checked,无论使用哪个jQuery,都不是手动完成的。

所以$("option[value='mister x']:first").attr("selected", "selected") 可以解决问题,如果只有一个列表,那么当存在多个具有唯一 ID 的表单时,这将如何工作 edit-form-# ?

ryanulit 的回答:

jQuery('#edit-form-'+id+' option[value='+assignee+']:first').attr("selected", "selected");

【问题讨论】:

  • 您能用 HTML 向我们展示您希望最终结果的样子吗?
  • 是什么导致 x 先生有两个选项?为什么一个大写而另一个不大写?
  • 您可以忽略大写字母,有两个选项可用,因此在长列表中,用户几乎可以立即选择他/她自己,而不必滚动列表并查找他/她自己的名字
  • 如果你只是先显示他们的名字然后列出其余不等于用户名的名字怎么办?这样您就不必担心区分这两个选项了。
  • 肯定会解决问题,但不能解决问题,我只想在绝对没有办法解决这个问题的情况下这样做。

标签: jquery list select option


【解决方案1】:

在选择框中checked="checked" 是一个常见错误,在html 部分应该是selected="selected"

思南。

【讨论】:

    【解决方案2】:

    我想选择第一个选项 选择与某个匹配的列表 价值...

    其中任何一个都应该为您做到这一点。当然,您可以将“mister x”替换为您想要搜索的任何值。

    $("option[value='mister x']:first").attr("selected", true);
    

    $("option[value='mister x']:first").attr("selected", "selected");
    

    编辑

    这是可能的。只需参考特定的选择元素。此外,我会将您选择的名称更改为 item_responsible ,就像其他人推荐的那样,并为其 ID 提供相同的文本。然后你可以这样做:

    $("#item_responsible option[value='mister x']:first") 而不是$("option[value='mister x']:first")

    ...#item_responsible 是您的选择:

    <select id="item_responsible" name="item_responsible">
         <!-- options here -->
    </select>
    

    【讨论】:

    • 谢谢,这是一个开始,可以改变它,所以我不必使用选项标签,因为它会改变页面上的所有选择框
    • 好吧!非常感谢,这个工作名称不是问题,并且 id 不能相同,因为有多个具有相同选项的选择框,不是我使用括号,但如果我没记错的话 PHP 受益于使用 []在处理表单数据时,我相信它会自动放入数组“项目”中。当然 id 可以是唯一的,但我不使用 select id。
    • 好的,那么要找到选择,您可能必须将其修改为 $("select[name='item[responsible]'] option[value...")。
    猜你喜欢
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2021-09-19
    • 2012-05-21
    • 1970-01-01
    相关资源
    最近更新 更多