【问题标题】:Select a radio control from Javascript从 Javascript 中选择一个无线电控件
【发布时间】:2017-12-26 12:08:23
【问题描述】:

我有一组 2 个 asp 无线电控件,我想在 ajax 回发中选择其中一个。我可以设置断点并且可以告诉代码正在被命中,但什么都没有发生。我已经尝试了 5-10 种不同的变体,但似乎没有任何效果。 .css 位是为了测试我是否可以找到正确的控件并且可以正常工作。 CSS 被应用。

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

 $("[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 输出

 <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 正在被应用。

【问题讨论】:

  • $("[main=true]").Checked = true; 应该是$("[main=true]").prop('checked', true);
  • 也试过了,不行。
  • 所以你的问题来自其他地方。我们不知道您如何尝试调用此代码。以及&lt;asp:RadioButton GroupName="Complete" runat="server" Text="No" main="true" /&gt;客户端是如何渲染的?
  • 页面加载时触发JS。我已经添加了完整的功能。

标签: javascript jquery input option


【解决方案1】:

我有一个困惑,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>

一个非常简单和幼稚的解决方案将是

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;
}
}
}

【讨论】:

  • 对不起,我有点老了。我的意思是asp文件,而不是背后的代码。
  • 不要认为它会起作用,控件是动态复制的,因此我使用自定义属性“main”来查找它们。
  • 如果元素是动态生成的,或者如果元素已经存在,那么当您将运行该函数以选择其中一个时,没有任何区别,我已经提供了一个主要的解决方案属性也。
  • 我认为您还应该提供生成的 HTML 代码,这肯定会帮助其他人消除错误
  • Jatin,已附上 HTMl。感谢您的评论,我想我看到了这个问题。主要属性正在成为输入控件之外的跨度。这可以解释为什么要应用 css。我想我的代码中需要 .(next)。
猜你喜欢
  • 2011-07-10
  • 2017-10-30
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
相关资源
最近更新 更多