JQuery select, .change and .click

I have a little problem with my script.

Purpose: I have select with several parameters, and if you select an option with the value "4", the selection will be deleted, and at this point I need to enter the input [type = text] and the word "remove" in the span tags, And now, when you click on this interval, the previously inserted input and range will be deleted, and in this place I need to place the previously deleted selection

I hope this makes sense :-)

here is my html code inside the form with id = f9:

 <label class="mandatory" for="ctrl_54">Title</label> <select class="select mandatory" id="ctrl_54" name="title"> <option value="1">Books</option> <option value="2">Movies</option> <option value="3">Events</option> <option value="4">other...</option> </select> 

and my js:

 function be_forms() { var myselect = $('form#f9 select#ctrl_54'); if ($('form#f9').length) { $('form#f9 select#ctrl_54').change(function() { if ($(this).val() == 4) { $('form#f9 select#ctrl_54').remove(); $('<input type="text" value="" class="text innyinput mandatory" id="ctrl_54" name="title" /><span class="remove">remove</span>').insertAfter('form#f9 label[for=ctrl_54]'); $("form#f9 span.remove").click(function(){ $('form#f9 input#ctrl_54').remove(); $('form#f9 span.remove').remove(); $(myselect).insertAfter('form#f9 label[for=ctrl_54]'); }); } }); } } 

And now almost everything works. I mean, when I select an option with a value of 4 - select deletes and the input and interval appear. When I click on this range, my selection appears again, and the input and interval are deleted. But now, if I select my choice again with a value of 4, nothing will happen. And I want to do it again - remove the selection, insert the input and range and so on ...

Sorry for my English, I'm not a native speaker.

+4
source share
2 answers

Here you should use .live() .

 $('#ctrl_54').live("change", function() { 
+2
source

As with jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() instead of .live() .

http://api.jquery.com/live/

+9
source

Source: https://habr.com/ru/post/1335240/


All Articles