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

javascript - Checkbox onchange function - Stack Overflow

programmeradmin1浏览0评论

I have a page with a set of checkbox's, that I want to run a Javascript function on when there is a change (I have done something very similar with dropdown's - and that worked)

However with the checkbox's I have three problems:

  1. my onChange event only runs "sometimes" (you have to change the focus between the different checkbox controls

  2. when it does run it is returning the result of the previous checkbox (not the one just clicked on)

  3. the jQuery always return the value true

Checkbox creation

<%= Html.CheckBox("sl-" + row.Id, value, new { onChange = "SuitabilityChecked("+row.Id+", "+key+")"})%>

Javascript

function SuitabilityChecked(providerId, parentRecordId) {
            var params = {};
            params.providerId = providerId;
            params.parentRecordId = parentRecordId;

            var value = $("#sl-" + providerId).val();               
            
            params.value = value;            
            $.getJSON("SuitabilityChecked", params, null);
        };

I have a page with a set of checkbox's, that I want to run a Javascript function on when there is a change (I have done something very similar with dropdown's - and that worked)

However with the checkbox's I have three problems:

  1. my onChange event only runs "sometimes" (you have to change the focus between the different checkbox controls

  2. when it does run it is returning the result of the previous checkbox (not the one just clicked on)

  3. the jQuery always return the value true

Checkbox creation

<%= Html.CheckBox("sl-" + row.Id, value, new { onChange = "SuitabilityChecked("+row.Id+", "+key+")"})%>

Javascript

function SuitabilityChecked(providerId, parentRecordId) {
            var params = {};
            params.providerId = providerId;
            params.parentRecordId = parentRecordId;

            var value = $("#sl-" + providerId).val();               
            
            params.value = value;            
            $.getJSON("SuitabilityChecked", params, null);
        };
Share Improve this question edited Jul 16, 2020 at 10:30 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 2, 2010 at 0:35 Grayson MitchellGrayson Mitchell 1,1874 gold badges24 silver badges44 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Browsers are funny about radio buttons and check boxes and can delay the onchange until focus change. Try adding an onclick event to blur or call the change event directly.

Maybe something like this using jQuery Live (untested, off the top of my head):

$(':checkbox').live('click', function() { $(this).change(); });

What's happening:

  1. Checkbox A clicked
  2. Checkbox B clicked
  3. Checkbox A has lost focus and fires onChange

Which makes it seem as if Checkbox B is returning the result of Checkbox A. If you were to press Tab after clicking Checkbox B in this scenario, you'd notice that its onChange would fire.

发布评论

评论列表(0)

  1. 暂无评论