简体   繁体   中英

How to display a result passed to a function in a textbox

i have a function the retrieves data from a database and returns it to a function.i am trying to get the Income value from the result to be displayed in the text box.

this is what i tried.

function Display(result) // result gets passed
{
 var income= $('#Income'); //getting the textbox;
income.value=result.Income  // when i try this result.Income,Income comes back as undefined.
 }

How do i display the value from my result into the textbox? this is how my "result" looks thats passed into the Display function.So ideally the value for Income is "Salary" so salary should be displayed in the textbox.

{
ID: 0
Number: 520
OtherIncome: "Private"
Income: "Salary"
}
This is my HTML
<input type="text" name="Income" id="Income" class="text ui-widget-content ui-corner-all" value="">

 var json = { ID: 0, Number: 520, OtherIncome: "Private", Income: "Salary", } function Display(result) // result gets passed { var income = $('#Income'); //getting the textbox; income.val(result.Income) // when i try this result.Income,Income comes back as undefined. } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" name="Income" id="Income" class="text ui-widget-content ui-corner-all" value=""> <button onClick="Display(json)">Click</button><br> Click the button to set the textbox value. 

Try using income.val(result.Income) to set the value of the text box. Here I have used a button click event to generate the example.

function Display(result) {
  var income = $('#Income'); //getting the textbox;
  //income.value=result.Income  here use the .val() method
  income.val(result.Income);
}

documentation: http://api.jquery.com/val/

Let's explain your code


var json = {
  ID: 0,
  Number: 520,
  OtherIncome: "Private",
  Income: "Salary",
}

function Display(result) // result gets passed
{
  var income = $('#Income'); //getting the textbox;
  income.val(result.Income) // when i try this result.Income,Income comes back as undefined.
}

you have an object of data stored in a variable json . Then a function Display(param) that takes a parameter. Then in your function, you were trying to access the empty parameter. The parameter result is a variable and it is not defined. So actually it has a value of undefined. hence why you get undefined when you invoke display without passing in any parameters. Assume you pass in a valid parameter in your function invoking then you would get the right result.

Example:


var json = {
  ID: 0,
  Number: 520,
  OtherIncome: "Private",
  Income: "Salary",
}


//invoke you function with the json data
Display(json)
//this should work perfectly.


//you can even remove the function and just make it

var income = $('#Income'); 
 income.val(json.Income);

//But you only do this if you want to get the value once



The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM