Using jQuery remove all the options of a select box and then add one option and select it

  • جي كويري

Using jQuery remove all the options of a select box and then add one option and select it.

الأجوبة

<!DOCTYPE html>
  <html>
  <head>
  <script src="https://code.jquery.com/jquery-git.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Remove all the options of a select box and then add one option and select it with jQuery</title>
  </head>
  <body>
  <select id='myColor'>
  <option value="Red">Red</option>
  <option value="Blue">Blue</option>
  <option value="Green">Green</option>
  </select>
<input type="button" value="Click to Remove all Options" onclick="Remove_options()"/>
  </body>
  </html>

JavaScript Code :

function Remove_options()
{
$('#myColor')
    .empty()
    .append('<option selected="selected" value="test">White</option>');
}
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...