jquery模态对话框通过id获取输入类型text的值


jquery Modal dialog Get value of input type text by id

我的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");

但仍然没有结果

如果您对想要获取值的代码使用jQuery语法,效果会更好:例如:

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

请确保您已经添加了jquery库,它不会与此冲突。

如果你想在按钮点击选项上获得这个值,那么你必须调用如下操作事件:

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

在本文中,您还需要在jQuery语法之间获取值。希望你能找到解决方案。