lundi 13 mai 2019

How to display a textbox if "others" option from checkbox group is checked

I want to display a text box using JavaScript when the "Other" option is checked from a group of checkboxes. I'm using razor pages with asp.net core 2.2

I'm using the following script but it is not working.

   function onSelectChange() {
        var sel = document.getElementById('QuestionOptionId');
        var strUser = sel.options[sel.selectedIndex].text; 


       if (strUser.startsWith('Other'))
                document.getElementById('textBox').disabled = false;          

       else
            document.getElementById('textBox').disabled = true;  
    }
</script>

Fruits.cshtml ( razor page view file)

        @foreach (var option in Model.QuestionOptions)
        {
            <input type="checkbox"  asp-for="QuestionOptionId"   id ="QuestionOptionId"  value ="@option.Value" onclick="onSelectChange()" />  @option.Text<br />              
        }

QuestionOptions is a selectList:

Value=1 Text= Apple, 
Value=2 Text= Kiwi, 
Value=3 Text =Other

whenever the user checks the checkbox for "Other", it should display a textbox. What am I doing wrong here? I have read so many other answers but not able to figure out this one.




Aucun commentaire:

Enregistrer un commentaire