开发者

putting text in a textbox when 2 checkboxes are checked

I have 3 webforms controls: 3 checkboxes and 1 textbox.

When I check checkbox1 and checkbox2, t开发者_JS百科hen in the textbox it should appear as 1,2.

How can this be done using ASP.NET webforms controls?


The easiest way would be to use JavaScript to modify the contents of the textbox.

<script language="javascript">
    var c1 = document.getElementById('<%=myCheckBox1.ClientID%>');
    var c2 = document.getElementById('<%=myCheckBox2.ClientID%>');
    var c3 = document.getElementById('<%=myCheckBox3.ClientID%>');
    var t = document.getElementById('<%=myTextBox.ClientID%>');

    function setTextBox()
    {
        if(c1.checked) t.value = "1";
        if(c2.checked) t.length > 0 ? ",2" : "2";
        if(c3.checked) t.length > 0 ? ",3" : "3";
    }
</script>

Then for your checkboxes add the following snippet:

onClientClick="setTextBox()"

EDIT: If you choose to use jquery, some of this code can probably be reduced/simplified. If you need to make the logic for what gets displayed a little more complicated (such as changing the textbox value when 1 and 2 are clicked but 3 is not, etc) this can all be accomplished with JavaScript in setTextBox().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜