javascript - 如何将选择元素设置为只读 ('disabled' 不会在服务器上传递选择值)

标签 javascript jquery select readonly

当我使用以下任何代码时,select 元素看起来确实被禁用,但选择并未在服务器上传递:我认为 readonly 是使用过,但我不知道或者是否可以解决问题。非常感谢任何帮助。

$('#selectID').prop('disabled',true);

$('#selectID').prop('disabled','disabled');

$('#selectID').attr('disabled',true);

$('#selectID').attr('disabled','disabled');

最佳答案

参见this answer -HTML form readonly SELECT tag/input

You should keep the select element disabled but also add another hidden input with the same name and value.

If you reenable your SELECT, you should copy it's value to the hidden input in an onchange event.

参见this fiddle演示如何将禁用选择中的选定值提取到将在表单中提交的隐藏字段中。

$(function() {
  var select_val = $('#sel_test option:selected').val();
  $('#hdn_test').val(select_val);
  $('#output').text('Selected value is: ' + select_val);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select disabled="disabled" id="sel_test">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

<input type="hidden" id="hdn_test" />
<div id="output"></div>

希望有帮助。

关于javascript - 如何将选择元素设置为只读 ('disabled' 不会在服务器上传递选择值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25301034/

相关文章:

javascript - 显示 12 小时制和 24 小时制时间

javascript - 由于目标被视为被动,因此无法阻止被动事件监听器内的默认设置 - Chrome

javascript - Node util.inherits 和这个问题

javascript - 用于传输命名参数和正文的云端点资源属性不起作用

javascript - 仅连接并打印带有文本的值,并在之间添加逗号

javascript - jQuery 将选择器限制为其父级

MySQLI SELECT 具有用于纪元时间戳选择的多个子句

MYSQL查询问题和子查询

Mysql 查询 LEFT JOIN 意外结果

javascript - 如何用 jQuery 包装内容?