0

i need to enable submit button when user select an option and this is a dynamic data. my HTML:

<form method="post" action="xxx" id="member">
    <tr>
        <td>1</td>
        <select name="status">
            <option value="a">aktif</option>
            <option value="l">terkunci</option>
            <option value="b" selected="b">blokir</option>
        </select>
        </td>
        <input type="hidden" name="update_buyer" value="TRUE" />
        <input type="hidden" name="id" value="1" />
        <td>
            <button role="button" data-toggle="modal" data-target="#modal" class="btn btn-danger delete">Delete</button>&nbsp;
            <input type="submit" class="btn btn-primary save" disabled="disabled" value="Save">
        </td>
    </tr>
    </form>
        <form method="post" action="xxx" id="member">
    <tr>
        <td>2</td> 
        <td>
        <select name="status">
            <option value="a" selected="a">aktif</option>
            <option value="l">terkunci</option>
            <option value="b">blokir</option>
        </select>
        </td>
        <input type="hidden" name="update_buyer" value="TRUE" />
        <input type="hidden" name="id" value="2" />
        <td>
            <button role="button" data-toggle="modal" data-target="#modal" class="btn btn-danger delete" data-email="[email protected]">Delete</button>&nbsp;
            <input type="submit" class="btn btn-primary save" disabled="disabled" value="Save">
        </td>
    </tr>
    </form>

i try

$('select').change(function(){
$('.save').removeAttr('disabled');
}

but it enable all submit button, i just want to enable which user click, not all submit button.

maybe like

$('select').change(function(){
$(this).parent('form').find('.save').removeAttr('disabled');
}

but it doesn't work :( how do i do this? help...

7 Answers 7

2

use .prop():

$('select').change(function(){
  $(this).parent().siblings().find('.save').prop('disabled', false);
});

Note:

You have same id for both forms this can cause issue and its invalid to use this way. ID should be unique to elems on a single page.

Sign up to request clarification or add additional context in comments.

1 Comment

EDIT: i use foreach for form ID's and it produce unique form like "member1,member2,...memberX" anyway thanks :)
1

try this one -

$(document).ready(function(){
  $('select').change(function(){
       $('.save').removeAttr('disabled');        
  });
});

Comments

0

Try this

$('select').change(function(){
 $('.save').prop("disabled", false);
}

Hope this helps

Comments

0

your second code is working , however you have invalid HTML tags.. chck it out..

here is the working fiddle using your code.

$('select').change(function(){
  $(this).parent('form').find('.save').removeAttr('disabled');
});
-^----here

note: i could see you are missing bracket at the end of change function...(not sure if it is a typo)

Comments

0

JSFiddle here

$('select').change(function(){
   $(this).parent().children('.save').prop('disabled', false);
});

Comments

0

Give each of your forms different ID names.. they are both id="member", they need to be unique. Not sure that is your error.

This worked for me even with same ID's.

$('#member select[name=status]').change(function(){
    $('#member input[type="submit"]').removeAttr('disabled');
 })

Comments

0

.parent method only goes up the DOM tree once, so that code will only work if the form element is the imediate parent of the select element, in this case it isnt. Use .parents() instead.

$('select').change(function(){
  $(this).parents('form').find('.save').removeAttr('disabled');
}

Also, note that your first select is not inside a <td>

EDIT: Other suggestions using .parent() that seem to work happen to be taking advantage of your invalid HTML (tr without table, unopened td, etc), they will not work with proper html, either remove the rows and columns and use other elements or build proper valid tables (one table per form)

2 Comments

i put <table> before <form> tag. i just don't write in here :) btw thanks
The resulting html is still invalid, sadly you cant put <form> between rows, the browsers will usually push the form outside the table (with no contents), meaning that if you submit, no values will be passed with the form. I suggest inspecting the code with a developer tool, to see the DOM structure the browser has built for your html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.