简体   繁体   中英

jquery Modal dialog Get value of input type text by id

I have this code in my php file

$text .= '      <input type="text" name="prefix" value="'.htmlspecialchars($e1['prefix']).'" maxlength="20" class="inputListForm" id="prefix"/>';

i want to get the value of the input type text

and this is my code :

var prefix = $('#prefix').get(0).value;
console.log(prefix);

i also try document.getElementById("prefix");

but still no result

It will be better if you use jQuery syntax for code you want get value: for example:

$(document).ready(function(){

   var prefix = $('#prefix').val();
   console.log(prefix); 

});

Make sure you have added jquery Library and it will not conflict this .

If you want to get this value on button click option than you must call action event as bellow:

$(document).ready(function(){
  $("#button").click(function (e) { 
      var prefix = $('#prefix').val();
       console.log(prefix); 
   });
});

In this you also need this get value between jQuery syntax. Hope you got your solution.

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