简体   繁体   English

如何使用JavaScript检查下拉菜单的选定值是否设置为No且文本框为空

[英]How to check if the selected value of drop-down is set to No and Text-Box is blank using JavaScript

I have drop-down and text-box in a gridview and I am trying to check if the selected value of a drop-down is set to "No" and no comments is entered in the text-box then i want to show message. 我在gridview中有一个下拉菜单和文本框,我试图检查下拉菜单的选定值是否设置为“否”,并且在文本框中未输入任何注释,然后我想显示消息。 The requirement should be as long as the selected value of the drop down is set to No then comments must be entered in the text-box. 只要下拉列表的选定值设置为“否”,该要求就必须存在,然后必须在文本框中输入注释。 My issue is that i am getting the message even if the drop-down is set to Yes or comments is provided when the drop down is set to No. Here is the code: 我的问题是,即使下拉菜单设置为“是”,或者当下拉菜单设置为“否”时,我仍会收到消息。这是代码:

    function validate() {
        var flag = false;
        var gridView = document.getElementById('<%= GridView1.ClientID %>');

        for (var i = 1; i < gridView.rows.length; i++) {
            var ddl = gridView.rows[i].getElementsByTagName('Select');
            var areas = gridView.rows[i].getElementsByTagName('textarea');
            if (ddl != null && ddl.length > 1 && ddl[0] != null && areas != null && areas.length > 1 && areas[0] != null) {
                if (areas[0].type == "textarea" && ddl[0].type == "select-one") {
                    var txtval = areas[0].value;
                    var txtddl = ddl[0].value;
                    if (txtddl.value == "No" && (txtval == "" || txtval == null)) {

                        flag = false;
                        break;
                    }
                    else {
                        flag = true

                    }
                }
            }
        }
        if (!flag) {
            alert('Please note that comments is required if drop down is set to No.  Thanks');
            areas[i].focus();
        }
        return flag;
    }

</script>

You can do like this: 您可以这样:

<script>
    function validate() {
        var flag = false;
        var gridView = document.getElementById('<%= GridView1.ClientID %>');

        for (var i = 1; i < gridView.rows.length; i++) {
            var selects = gridView.rows[i].getElementsByTagName('select');
            //var inputs = gridView.rows[i].getElementsByTagName('input');
            var areas = gridView.rows[i].getElementsByTagName('textarea');
            if (selects != null && areas != null) {
                if (areas[0].type == "textarea") {
                    var txtval = areas[0].value;
                    var selectval = selects[0].value;
                    if (selectval == "No" && (txtval == "" || txtval == null)) {

                        flag = false;
                        break;
                    }
                    else {
                        flag = true;
                    }
                }
            }
        }
        if (!flag) {
            alert('Please enter comments.  Thanks');

        }
        return flag;
    }
</script>

Try this: 尝试这个:

$('#select').on('change', function () {

   var text = $(this).find(":selected").text();
   var textBox = $(this).parent().filter('input [name="InputName"]');
   if(text == "no" && textBox.val() =="")
   {
      \\display your message
   }
});

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

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