Solved

jquery, dropdown

Posted on 2016-09-08
4
70 Views
Last Modified: 2016-11-15
function OnSuccess(response) {

         var productSubCode = '#' + '<%=PRODUCT_SUBCODE.ClientID %>';
         var sc = $(productSubCode).empty();
         $.each(response.d, function (i, d) {
             sc.append($('<option/>').val(d).html(d));           
         });
         //alert("Success: " + response.d);
     }

Open in new window


It seems like OnSuccess return me  <select><option>Test</option></select> only.
How can I get a point return me below:

<select><option value="Test">Test</option></select>
0
Comment
Question by:ITsolutionWizard
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41790079
There must be something else wrong.
This code using your javascript works perfectly. How are you examining the created <option>'s?
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery.js"></script>
</head>
<body>
<select id="test"></select>
<script>
function OnSuccess(response) {
  var sc = $('#test').empty();
  $.each(response.d, function (i, d) {
    sc.append($('<option/>').val(d).html(d));          
  });
}
var data = {
   d: [
      'test1',
    'test2',
    'test3'
   ]
}
OnSuccess(data);
</script>
</body>
</html>

Open in new window


Working sample here
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41792053
try this one :

function OnSuccess(response) {

         var productSubCode = '#' + '<%=PRODUCT_SUBCODE.ClientID %>';
         var sc = $(productSubCode).empty();
         var jsonString = response.d;
         var jsonObject = $.parseJSON(jsonString);
         $.each(jsonObj, function (i, d) {
             sc.append($('<option/>').val(d).html(d));           
         });
         //alert("Success: " + response.d);
     }

Open in new window


optimized(?) version :
function OnSuccess(response) {

         var productSubCode = '#' + '<%=PRODUCT_SUBCODE.ClientID %>';
         var jsonString = response.d;
         var jsonObject = $.parseJSON(jsonString); // please note $.parseJSON is deprecated since jquery 3.0
         var sc = new Array();
         for(var i=0;i<jsonObject.length;i++)
         {
             var d = jsonObject[i];
             sc.push("<option value='" + d +"'> + d + "</option>");           
         };
         $(productSubCode).html(sc.join(""));
         //alert("Success: " + response.d);
     }

Open in new window

0
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41792274
We are missing a bit of the puzzle here
You have this
$.each(response.d, function (i, d) {
  sc.append($('<option/>').val(d).html(d));           
});

Open in new window

Which produces this
<select><option>Test</option></select>

Open in new window

So 'd' in the loop is evaluating to Test. If d was an unparsed JSON string we should still see the full JSON string in the value.

Can you do some screen grabs (if you can't send us a link)
1. Right click the Select and select inspect element (Show us the HTML for this)
2. Click the console tab and show us the returned response from the server

Can you also show us the code that is generating the response.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41889193
The accepted answer ask questions when my last answer solve the issue especially my optimised version.
We know .Net return : {d:"the json string"}
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question