【问题标题】:How to executed an radio onchange event based another event?如何基于另一个事件执行广播 onchange 事件?
【发布时间】:2019-01-27 04:26:16
【问题描述】:

我在一个表格中有几个单选按钮。

<table id='t_test'>
<tr>
   <td><input type='radio' id='radio1' value='0' class='form-control' /></td>
   <td><input type='radio' id='radio2' value='50' class='form-control' /></td>
   <td><input type='radio' id='radio3' value='100' class='form-control' /></td>
</tr>
</table>
<input type='button' id='btn_radio' />

我在这些单选按钮上有一个onchange 事件。

$("#t_test").on('change','input[type=radio]',function(){
    alert($(this).val());
});

如何在onchange事件上触发另一个事件?我有这个按钮点击事件。

$('#btn_radio').on('click', function(){
     var i = 50;

     if(i == 0){
        $("#radio1").prop("checked", true).trigger("change");
    }
    else if(i == 50){
        $("#radio2").prop("checked", true).trigger("change");
    }
    else if(i == 100){
        $("#radio3").prop("checked", true).trigger("change");
    }       
    else{
        alert("Error Occured");
    }


});

所以,当我单击按钮时,会检查 id 为 radio2 的收音机并触发警报。

【问题讨论】:

  • 您的代码运行良好。见this fiddle
  • 这是按钮 ID。
  • 但是,为什么没有触发无线电警报?
  • 有什么问题?
  • 警报未触发。

标签: javascript jquery onclick onchange radio


【解决方案1】:

为您的按钮添加名称。

当然,检查后没有警报。这是因为当所有的收音机都检查过了。没有更多的变化触发器,除了点击触发器。

或者您不希望这台收音机在组中(同名)?

<table id='t_test'>
  <tr>
    <td><input type='radio' name='radio' id='radio1' value='0' class='form-control' /></td>
    <td><input type='radio' name='radio' id='radio2' value='50' class='form-control' /></td>
    <td><input type='radio' name='radio' id='radio3' value='100' class='form-control' /></td>
  </tr>
</table>
<input type='button' id='btn_radio' />

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 1970-01-01
    • 2010-09-20
    • 2016-02-21
    • 2015-10-20
    • 2015-01-02
    • 1970-01-01
    • 2016-01-25
    • 2016-06-12
    相关资源
    最近更新 更多