Change the Text of an asp:CheckBox when clicked

丶灬走出姿态 提交于 2019-12-22 09:16:22

问题


How can I change the Text for a CheckBox without a postback?

<asp:CheckBox ID="CheckBox1" runat="server" Text="Open" />

I would like to toggle the Text to read "Open" or "Closed" when the CheckBox is clicked on the client.


回答1:


function changeCheckboxText(checkbox)
{
  if (checkbox.checked)
    checkbox.nextSibling.innerHTML = 'on text';
  else
    checkbox.nextSibling.innerHTML = 'off text';
}

called as:

<asp:CheckBox runat="server" ID="chkTest" onclick="changeCheckboxText(this);" />

Just FYI, it's usually bad practice to change the text of the checkbox label because it tends to confuse the user.




回答2:


If you're interested to use a framework javascript like jQuery, i propose a solution ilke this:

$("input[id$=CheckBox1]").click(function() {
    if ($(this).attr("checked")) { 
        $(this).next("label:first").text("Open");
    }
    else {
        $(this).next("label:first").text("Close");
    }
});



回答3:


3 Easy Options

Use JavaScript to change the text client side- By Registering the Event in the .CS class like so:

CheckBox1.Attributes.Add("JavaScipt")

Use an HTML Checkbox with JQuery

Or put in an an Ajax Update Panel

Some AJax starter Videos



来源:https://stackoverflow.com/questions/429937/change-the-text-of-an-aspcheckbox-when-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!