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()
.
精彩评论