Setting a select by label via jquery in 1.4

前端 未结 4 1042
遥遥无期
遥遥无期 2021-01-12 15:24

In jquery 1.3.2, the following works:


                        
    
提交评论

  • 2021-01-12 16:10

    Try this code:

    $('#c').val("325");
    
    0 讨论(0)
  • 2021-01-12 16:14
    $('#c').val('Red');
    

    shouldn't have (imho) worked in jQuery 1.3 because "Red" isn't the value. "325" is. What does this do:

    $('#c').val("325");
    
    0 讨论(0)
  • 2021-01-12 16:19
    $('#c').val('325');
    

    or

    // earlier - define a text-equals selector
    jQuery.extend(jQuery.expr[":"], {
      "text-equals": function (a, i, m) {
        return (a.textContent||a.innerText||jQuery(a).text()||'')==m[3];
      }
    });
    
    // later - use it
    $red = $('#c option:text-equals(Red)');
    $('#c').val($red.val());
    

    The custom selector is one possibility. You could also do exactly the same thing in a filter() callback, for example.

    0 讨论(0)
  • 提交回复
    热议问题