最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Javascript change text of Radio button - Stack Overflow

programmeradmin9浏览0评论

I am using following Method in Javascript.

Function JsChangeRBText(itemCheckBox, rbClientId)
{
   var optVal = document.getElementById(rbClientId);
   if(itemCheckjBox.checked)
   {
        optVal.innerText = 'abc';
    }       
   else
   {
        optVal.innerText = 'xyz';
    }       
}

here rbclientId is client id of radio button. I want to change its text on a check box checked or unchecked condition. But using above code I am unable to it.Am I missing something. I also tried for innerHTML. When I am using alert to get the current text using innerText and innerHTML, both are blank. But in alert I can see rbClientId's id and value.

I am using following Method in Javascript.

Function JsChangeRBText(itemCheckBox, rbClientId)
{
   var optVal = document.getElementById(rbClientId);
   if(itemCheckjBox.checked)
   {
        optVal.innerText = 'abc';
    }       
   else
   {
        optVal.innerText = 'xyz';
    }       
}

here rbclientId is client id of radio button. I want to change its text on a check box checked or unchecked condition. But using above code I am unable to it.Am I missing something. I also tried for innerHTML. When I am using alert to get the current text using innerText and innerHTML, both are blank. But in alert I can see rbClientId's id and value.

Share Improve this question asked Mar 8, 2013 at 6:37 PramodPramod 6675 gold badges18 silver badges36 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3
function JsChangeRBText(itemCheckBox, rbClientId)
{
   var optVal = document.getElementById(rbClientId);
   var label =  optVal.nextSibling;
   if(itemCheckjBox.checked)
   {
     label.value = "adc";;
   }       
   else
   {
     label.value = "ccc";
   }       
}

HTML:

<input type="radio" /><label>Option 1</label>

or

<input type="radio" /><span>Option 1</span>

JS:

var $label = $('input[type=radio]').next();
$label.text('Options'); 

Firstly declare radio button like this

<input type="radio" id="rb" runat="server"/>
<label for="<%=rb.ClientID%>">option 1</label>

I mean use html radio button with runat="server" rather than asp:RadioButton, this will ensure rendering of control as it is and then try this

function JsChangeRBText(itemCheckBox, rbClientId)
{
   var optVal = document.getElementById(rbClientId);
   if(itemCheckjBox.checked)
   {
       optVal.nextSibling.value = 'abc';
    }       
   else
   {
       optVal.nextSibling.value = 'xyz';
    }       
}

i know that question has been resolved but posting this solution in case it is useful for other.

But here i have used Jquery

<script src="http://code.jquery./jquery-1.9.1.min.js"></script>
<script>
    Function JsChangeRBText(itemCheckBox, rbClientId)
    {
       var optVal = document.getElementById(rbClientId);
       if(itemCheckjBox.checked)
       {
            $("label[for='" + rbClientId + "']").text('abc');
        }       
       else
       {
            $("label[for='" + rbClientId + "']").text('xyz');
        }       
    }
</script>

Not tested but it I guess this should work. Please do reply if it is not working.

<asp:CheckBox
         ID="CheckBox1"
         runat="server"
         Text="Set Radio Checked"
         OnCheckedChanged="setText(this, document.getElementById('RadioButton1'))"
         AutoPostBack="true"
 />
<asp:RadioButton   
         ID="RadioButton1"   
         runat="server"  
         Text="Red"  
         GroupName="group1" 
/>

<script runat="server">  
    protected void setText(checkBox, radioBtn)  
    {  
        if (checkBox.Checked == true) {  
            radioBtn.Text = "abc";
        }  
        else{  
            radioBtn.Text = "xyz";
        }
    }  
</script>  
function JsChangeRBText(itemCheckBox, rbClientId)
{
   var optVal = document.getElementById(rbClientId);
   var label =  optVal.nextSibling;
   if(itemCheckjBox.checked)
   {
     label.textContent = "adc"; 
   }       
   else
   {
     label.textContent = "ccc";
   }       
}

Use textContent. This work for me.

发布评论

评论列表(0)

  1. 暂无评论