jquery模式对话框通过id获取输入类型文本的值

时间:2016-04-07 09:48:54

标签: javascript php jquery

我的php文件中有这段代码

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

我想获取输入类型文本的值

这是我的代码:

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

我也尝试document.getElementById("prefix");

但仍然没有结果

1 个答案:

答案 0 :(得分:0)

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.