Select defaul selected value

Given this HTML:
<select>
    <option value="0">One</option>
    <option value="1">Two</option>
</select>
Select by description for jQuery v1.6+:
var text1 = 'Two';
$("select option").filter(function() {
    //may want to use $.trim in here
    return $(this).text() == text1; 
}).prop('selected', true);
Select by description for jQuery versions below 1.6 and greater than or equal to 1.4:http://stackoverflow.com/a/3644500/31532
var text1 = 'Two';
$("select option").filter(function() {
    //may want to use $.trim in here
    return $(this).text() == text1; 
}).attr('selected', true);
Note that while this approach will work in versions that are above 1.6 but less than 1.9, it has been deprecated since 1.6. It will not work in jQuery 1.9+.
Select by description for previous versions:
val() should handle both cases. Are you not seeing it?
Eg:
$('select').val('1'); // selects "Two"
$('select').val('Two'); // also selects "Two"

credit : http://stackoverflow.com/questions/496052/jquery-setting-the-selected-value-of-a-select-control-via-its-text-description

โพสต์ยอดนิยมจากบล็อกนี้

J2EE คืออะไร

วิธีแก้ : แก้ไขตารางแล้ว save ไม่ได้ ต้องสร้างใหม่ - Saving changes is not permitted in SQL Server Management Studio

การออกแบบ RESTful API - วิธีปฏิบัติที่ดี