开发者

Checkbox onclick not firing

开发者 https://www.devze.com 2022-12-22 12:58 出处:网络
I\'m at my wit\'s end with this. Can anyone see anything wrong with this line? The fun开发者_如何学编程ction won\'t fire by clicking on the checkbox for some reason, but the calling function works fi

I'm at my wit's end with this.

Can anyone see anything wrong with this line? The fun开发者_如何学编程ction won't fire by clicking on the checkbox for some reason, but the calling function works fine (if I copy the exact "onclick" attribute over to the label for the checkbox, it works fine).

<input type="checkbox" name="match_35_0" id="match_35_0d" value="d0" onclick="checkSwap(document.page_form.match_35_0d, document.page_form.match_35_0)"></input> 

If anyone can see why on earth this wouldn't be working, I would really appreciate it.

Thanks!

EDIT: Since a couple people asked, here's the checkSwap function (all it does is throw an alert so I can see that my onclicks are working before I add any code):

function checkSwap(radioid, groupid) {
alert("radio: " + radioid + " group: " + groupid);}

And here's the whole sample of the table cell that the checkbox in question is in (apologies for the formatting, the code sample doesn't seem to want to accept my newlines):

<td><label onclick="checkSwap(document.page_form.match_34_0d,document.page_form.match_34_0)" for="match_34_0">N</label><input type="checkbox" name="match_34_0" id="match_34_0d" value="d1"  onclick="checkSwap(document.page_form.match_34_0d, document.page_form.match_34_0)"></input></td>

EDIT: Alright, canceling out a separate function that was limiting the checkboxgroup to 1 checked box was the issue.

The code that does the limiting was setting an onclick attribute for each checkbox, and that is apparently overriding the tag-set attribute. I'll have to figure out how to hack around it.


This syntax

document.page_form.match_35_0d

actually searches in the form with name of page_form for an input element with name of match_35_0d. But you have actually set it as an id of the checkbox where the onclick is definied.

You could solve your problem with the following call in the onclick:

checkSwap(this, document.page_form.match_35_0)

By the way, a checkbox is not the same as a radiobutton and you're actually not passing the ID's to the function, but instead the whole elements. Rewrite your function as

function checkSwap(checkbox, group) {
    var checkboxId = checkbox.id;
    for (var i = 0; i < group.length; i++) {
        var element = group[i];
        var elementId = element.id;
        // ...
    }
    // ...
}

To obtain an element by ID, just use Document#getElementById().

var element = document.getElementById('someId');


If JQuery's ready method is already defined then Chek box onclick event do not work. You can fire the event if you add a Jquery click event inside ready. Not sure if this is IE issue ..?


Incase you already have Jquery's ready function then Onclick attribute of the ckeckbox will not fire. You have to add the click event in Jquery. Only then it works Like below. I don't know the reason.

$('#cbFinalAttest').click(function (event) {  
   ...
}


this function does fire - checked in firebug

<input type="checkbox" name="match_35_0" id="match_35_0d" value="d0" onclick="alert('55')"></input>

you have to check 'checkSwap'


It would be easier to pass in this to the function, then the parameter would be a reference to the element that called the function. So you can do:

function checkSwap(self){
    alert(self.id);
}

Edit: Also, document.page_form.match_35_0.id will get the id, not the way you have it.

0

精彩评论

暂无评论...
验证码 换一张
取 消