在单选按钮上执行onChange功能

时间:2019-05-02 12:07:15

标签: javascript ajax onchange

ive得到了(有效的)以下代码

$('#selBookable').change(function () {...

 <div class="radio radio-search">
  <label><input id="radio-new-pop" type="radio" name="radio_newest_popular" value="new" >Neueste</label>

在#selBookable获取另一个值时执行。

现在,我想在单击其他地方的单选按钮时执行此功能。

我尝试过:

$('#selBookable, #radio-new-pop').change(function () {...

但这不起作用。 现在我想给这个“ .change(function NAMEHERE” 命名并执行该功能,但也不起作用。 任何帮助表示赞赏。

编辑:我尝试使用onchange事件,但是再次我没有要调用的函数名称。

3 个答案:

答案 0 :(得分:1)

您可以独立于函数的用途定义函数,并为其命名。然后,您可以将其附加到所需的多个目标上,并按有效的目标进行呼叫(使用名称代替定义),每个目标都有一个不同的目标。

(您第二次尝试不起作用的原因是$的第二个参数不是附加函数的另一个目标,而是对在哪里找到第一个目标的限制。)

答案 1 :(得分:1)

也许尝试将jquery侦听器放在val map = list.groupByDistinctValues({it.first}) { it.second } // where map is then of type Map<String, Set<Int>> 内。

OR

您可以在广播中添加$(document).ready()处理程序,如下所示:

click

和JS:

    <div class="radio radio-search">
        <label>
            <input id="radio-new-pop" type="radio" name="radio_newest_popular" value="new" onclick="handleClick(this)">
        Neueste</label>

改编自this答案。

答案 2 :(得分:1)

您有两个具有相同名称的单选按钮:

<div class="radio radio-search">
    <label>
        <input id="radio-new-pop" type="radio" name="radio_newest_popular" value="new" />
        Neueste
    </label>
    <label>
        <input id="radio-new-pop2" type="radio" name="radio_newest_popular" value="new" />
        Other
    </label>
</div>

添加一个javascript函数来处理此更改:

var onRadioButtonChange = void 0;
{
    onRadioButtonChange = function (e) {
        theFunctionYouWantToExecute(); // pass this or e.target if needed
        return false;
    }
}

将其附加到您的元素:

Array.prototype.forEach.call(
    document.querySelectorAll('input[type="radio"]'), 
    function (element) { 
        element.addEventListener('change', onRadioButtonChange, false); 
    }
);
相关问题