user2130712
user2130712

Reputation: 27

PHP array field with javascript

I'm using an array to add data to my database, and JavaScript to show and hide field depending on yes and no.

<input type="radio" id="medication1" name="medication[]" value="Yes" />
<input type="radio" id="medication2" name="medication[]" value="No" />


 $("#show").hide();
        $("input[name=medication]").click(function()
        {
            if ($("#medication1").attr('checked'))
                $("#show2").hide();
            if ($("#medication2").attr('checked'))
                $("#show2").show();
        });

Without the array block quotes it works perfectly but once I add them it doesn't. Is there maybe a way I can get around this?

Upvotes: 0

Views: 45

Answers (3)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

try this with ^= in your selector you tell to select all input with name that start with medication

$("#show").hide();
        $("input[name^=medication]").click(function()
        {
            if ($("#medication1").attr('checked'))
                $("#show2").hide();
            if ($("#medication2").attr('checked'))
                $("#show2").show();
        });

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

change

$("input[name=medication]").click(function()

to

$("input[name='medication[]']").click(function()

Upvotes: 0

gdoron
gdoron

Reputation: 150273

You need escape those special meanings chars: []:

$("input[name=medication\\[\\]]").click(function()

Source

Upvotes: 3

Related Questions