jquery gets all value and text instances of select option

  • 2021-07-26 06:30:49
  • OfStack

I have been learning jQuery for a few days, and today, due to the need of work, I need to put all value and option corresponding to select

text get out, and then splice, import into the xml file, the result of 1 afternoon, finally come out. Here is the corresponding code:

< body > The code in is copied from someone else's website. Now I need to get its value and text and splice them into

< Sunrise > 7 < /Chaoyang > Form of


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
 <script type="text/javascript" language="javascript" > 
 $(document).ready(function(){ // Waiting for all dom Execute after drawing is completed 
  
     var arr = new Array(); // Array definition standard form, do not write as Array arr = new Array();
     var all = ""; // Save all definition variables 
     $("#UserBorough option").each(function () {
       var txt = $(this).text(); // Get a single text
       var val = $(this).val(); // Get a single value
       var node = "<" + txt + ">" + val + "</" + txt + ">";
       arr.push(node);
       all += node;
     });
     alert(all);
     alert(arr);

  
 }); //$(document)
 </script>
</head>
<body>

 <select name="UserBorough" size="6" class="input" id="UserBorough" style="width:150px;" onchange="setSmallBorough('UserBorough','SmallBorough','/search_auto.aspx?searchtype=Borough&searchkey='+ this.options[this.selectedIndex].value +'','');">
            <option value=7> Sunrise </option><option value=1> Haidian </option><option value=4> Changping </option><option value=8> Fengtai </option><option value=14> Daxing </option><option value=2> East Side </option><option value=3> West City </option><option value=5> Chongwen </option><option value=6> Xuanwu </option><option value=12> Tongzhou </option><option value=13> Shunyi </option><option value=11> Fangshan </option><option value=9> Shijingshan </option><option value=10> Mentougou </option><option value=15> Huairou </option><option value=18> Clouds </option><option value=17> Yanqing </option><option value=16> Pinggu </option><option value=448> Peripheral </option>
</select>



</body>
</html>

Summary: Combining tools can save a lot of work. You can create a new html file in VS 2010, type the code, and then copy the code to

Dreamweaver, which saves time.

Through this example, I realized that if you just understand the code in the book and type it down, you will not be able to write something, but you still need to do more work

Write it yourself.


Related articles: