从JavaScript中选择一个无线电控件



我有一个由2个ASP无线电控件组成的组,我想在Ajax Postback上选择其中一个。我可以放置断点,并且可以说明代码正在命中,但什么也没有发生。我尝试了5-10种不同的变化,但似乎没有任何作用。.CSS位是为了测试我可以找到正确的控制,并且正在工作。CSS应用。

我尝试了.trigger("click"),也尝试了attr(checked="checked" and true)。尝试了我在Google上找到的任何东西。但是第二个选项没有被检查。

 $("[main=true]").Checked = true;
 $("[main=true]").css("background-color", "yellow");

html

<asp:RadioButton GroupName="Complete" runat="server" Text="Yes" Checked="true" />
<asp:RadioButton GroupName="Complete" runat="server" Text="No" main="true" /><br />

我似乎遇到的另一个小问题是,如果我单击单词"是"或"否"两个选项框都未选择。有什么原因?

        $(window).load(function () {
        $("[main=false]").prop('checked', false);
        $("[main=true]").prop('checked', true);
        $("[main=true]").prop('checked', true);
        $("[main=true]").attr('checked', true);
        $("[main=true]").Checked = true;
        $("[main=true]").css("background-color", "yellow");
        var id = localStorage.getItem("CustomerID");
        $('img[alt = "' + id + '"]').trigger("click");
        localStorage.clear();
    });

html ut put

 <span>Mark as complete:</span><span main="false">
 <input id="gvCustomers_ctl07_0" type="radio" name="gvCustomers$ctl02   $Complete" value="ctl07" checked="checked" />
   <label for="gvCustomers_ctl07_0">Yes</label>
  </span><span main="true"><input id="gvCustomers_ctl08_0" type="radio"     name="gvCustomers$ctl02$Complete" value="ctl08" />
      <label for="gvCustomers_ctl08_0">No</label></span>

顺便说一句,有一个以上的无线电控件将主要属性设置为true,但我希望所有无线电控件都被选中。不知道这是否有任何区别。没有意义的是CSS正在应用。

我感到困惑,html如何

<asp:RadioButton GroupName="Complete" runat="server" Text="Yes" Checked="true" />

据我所知,无论您使用哪种编程语言,它首先转换为HTML,然后在浏览器中渲染。

我可以想象,您的代码将被转换为

<input name='Complete' type='radio'  checked='true'></input>
<input name='Complete' type='radio' main='true'></input>

非常简单&amp;幼稚解决您的问题将是

var elemArray=document.getElementsByName("Complete");
elemArray[0].checked=true;//For 1 radio
elemArray[1].checked=true; //For 2 radio

,或者您可以简单地迭代元素数组

for(i=0;i<elem.length;i++){
if(elem[i].hasAttribute('main')){
elem[i].checked=true;//if the second element do not have any main attribute
//or
if(elem[i].main='true'){
elem[i].checked=true;
}
}
}

最新更新