html - 如何在选择列表中设置标题和默认值?

标签 html css forms drop-down-menu

我正在寻找一种方法来设置默认值(在选择列表中),必须将其关闭才能接受 'required=""' 属性并提交表单。换句话说,默认值不能保持为所选值。此外,如何将无法选择的标题添加到选择列表中,并将其作为标记选择选项的一种方式。下图显示了我要查找的所有内容,以防混淆。

enter image description here

最佳答案

In other words the default value can't stay as the chosen value.

关于此请求,您会发现它很有帮助。

<form action="/action_page.php">
<select id="mySelect" required onchange="changeValue()">
  <option value="">Select a Country</option>
  <option value="c1">Country 1</option>
  <option selected value="c2">Country 2</option>
  <option value="c3">Country 3</option>
  <option value="c4">COuntry 4</option>
</select>
<input id="btnSubmit" type="submit" disabled>
</form>

<script>
function changeValue(){
    if (document.getElementById("mySelect").value != "")
        document.getElementById("btnSubmit").disabled = false;
}
</script>

如果您选择的值发生变化(就在第一次之后)并且与 "" 不同,则提交按钮将有效。这也可以通过标志而不是设置属性禁用来完成,这取决于您需要做什么。

关于html - 如何在选择列表中设置标题和默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45383397/

相关文章:

html - 即使未满足最小长度也会提交表单

javascript - 如何使用 html-minifier npm 模块?

javascript - 使用实时数据对来自 GeoJSON 文件的大量标记进行样式化

html - css 动画旋转 div 也将文本旋转 180 度

css - 选择当前 div 上方的同级 div

javascript - Material ui - 无法使用 makeStyles 覆盖日期选择器样式

javascript - HTML5 : input type duration

html - 网站显示正方形而不是字符?

php - 获取我的表单数据以发布到 mysql 数据库

javascript - 当我单击表单中没有的按钮时,Jquery Ajax 提交表单