samedi 15 août 2015

Run a function when submitting a form failing

I have the following set of checkboxes in a webpage

<form id="tagselect" action="">
<input id="tag" type="checkbox" value="*">Show all</input>
<input id="tag" type="checkbox" value=".Name1">Name1</input>
<input id="tag" type="checkbox" value=".Name2">Name2</input>
<input type="button" value="submit"></input>
</form>

I want to make it so a function runs either when the submit button is pressed, or remove the submit button and make it so it runs any time any checkbox is changed.

I have tried pretty much every different way to achieve this as recommended in this old question here: run a function after checkbox checked and pushed submit button with jquery along with a few different tweaks of them just o a random hunch, and I cannot get ANYTHING to happen when a checkbox is checked except the checkmark appearing. I tested with a simple:

alert("This is working");

and it worked fine when it was just running in a script block with no conditions, but as soon as i put it in the middle of some script to make it run when a checkbox is checked or a button is pushed, the alert won't appear no matter what.

A few of the things I've tried:

<script>
$("input[id='tag']:checked").each(
    function() {
        alert("This is working");
    }
);
</script>

Attempt 2:

<script>
$("input#tag :checked").each(
    function() {
        alert("This is working");
    }
);  
</script>

Attempt 3:

<script>
$('#tagselect').submit(function(){
$('#tag:checked').each(function()
      {
         alert("This is working");
      })
 });
</script>

Attempt 4:

<script>
$('input[value=submit]').on('click', myFunction);

Then this in either a new script block or the same script block(tried both):

function myFunction() {
alert("This is working");
}
</script>

I also tried that same myFunction, but with

onclick="myFunction()"

added to the submit button html

Absolutely nothing visibly happened besides the button clicking or the checkboxes checking on any of these runs.

I have also tried putting all the scripts together in the head, together in the body, separately around where they are going to be used, ad i have triple checked spelling and capitalization is consistent in the actual copies. I have removed the submit button and tried some of these without it. I have tried switching browsers in case it was a compatibility issue. I don't know enough about JS to even know where to start researching what I'm doing wrong.




Aucun commentaire:

Enregistrer un commentaire