简体   繁体   English

如何验证是否选择了选择HTML元素的选项

[英]How to validate if an option of select html element is selected

I have a javascript function that deletes the selected option/options from the select html element. 我有一个javascript函数,可从select html元素中删除所选的一个或多个选项。

function fn_delete()
{
    var result = confirm("Are you sure?");
    if (result) 
    { 
        var x = document.getElementById("t2");
        x.remove(x.selectedIndex);
        var i;
        for(i=0; i<t2.length; i++)
        {
            if(t2.options[i].selected)
            {
                t2.remove(i);
            }
        }
   }
}

The problem is that even if the user does not select any of the options and click on delete, still he gets the confimation message of "Are you sure". 问题是,即使用户没有选择任何选项并单击“删除”,他仍然会收到“您确定”的确认消息。 what is the condition to validate if any option is selected at all or not and how to implement it? 究竟是否选择了任何选项,验证的条件是什么?如何实施?

You're just a little off in your logic .. Call the function .. THEN ask "Are you Sure" IF it is SELECTED 你只是一个小关在你的逻辑..调用函数... THEN问:“你确定” IFSELECTED

EDITED Here is the FIDDLE 编辑这里是FIDDLE

    <input type="checkbox" id="t2">

<div id="remove" onclick="fn_delete()">
Remove
</div>
<script>
function fn_delete() {

  var t2 = document.getElementById("t2");

  var i;
  //alert (t2.length);
  //for (i = 0; i < t2.length; i++) {
  //alert('made for');
    if (t2.checked) {
      var result = confirm("Are you sure?");
      if (result) {
        //t2.remove(i);
        t2.remove(t2.selectedIndex);
      }
    }
  //}

}
</script>

Actually, by default, the first option may be selected, if the page reloads etc. Thus introducing a "nothing" or "null" option and then check against ndex is greater zero will help. 实际上,默认情况下,如果页面重新加载等情况,则可以选择第一个选项。因此,引入“ nothing”或“ null”选项,然后检查ndex是否大于零将有所帮助。 You may delete the alert(... stuff obviously 您可以删除alert(...显然是

 function fn_delete() { var ch = document.getElementById("t2").selectedIndex; alert(ch); if (ch > 0) { var result = confirm("Are you sure?"); if (result) { var x = document.getElementById("t2"); x.remove(x.selectedIndex); var i; for (i = 0; i < t2.length; i++) { if (t2.options[i].selected) { t2.remove(i); } } } } } 
 <select id="t2"> <option>nothing</option> <option>one</option> <option>two</option> <option>three</option> </select> <input type="button" onClick="fn_delete();" value="check"> 

You could put an onchange attribute on your select element to enable your button only after an option has been selected. 您可以在select元素上放置onchange属性,以仅在选择选项后启用按钮。

You could really do this a number of ways though. 您确实可以通过多种方法来执行此操作。 You could also have a click handler on your button that checks if there's a selection to begin with, as another user suggested. 您还可以在按钮上有一个单击处理程序,以检查是否有开始的选择,如另一个用户建议的那样。 I prefer to disable buttons when things are not in a validate state to click them. 当事物未处于验证状态时,我更喜欢禁用按钮以单击它们。

 // checks if any options are selected, and enables/disables the Delete button as needed function checkSelected() { var select = document.getElementById("t2"), deleteBtn = document.getElementById("delete"), hasSelected = false; for (var i = 0; i < select.options.length; i++) { if (select.options[i].selected) { hasSelected = true; break; } } deleteBtn.disabled = hasSelected ? '' : 'disabled'; } // removes the selected options from the list function fn_delete(optlist) { var x = document.getElementById("t2"); if (confirm("Are you sure?")) { x.remove(x.selectedIndex); for (var i = 0; i < t2.length; i++) { if (t2.options[i].selected) { t2.remove(i); } } } } 
 <select multiple id="t2" onchange="checkSelected()"> <option>One</option> <option>Two</option> <option>Three</option> </select> <br> <br> <input type="button" id="delete" value="Delete Selected" disabled onclick="fn_delete()"> 

selectedIndex equals to -1 if no option is selected: 的selectedIndex等于-1,如果未选择任何选项:

if (x.selectedIndex == -1) {
// please, make a selection
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM