开发者

jQuery live problem after attaching ajax-generated content

I'm trying to generate a form dynamically using jQuery-ajax and PHP. For the inputs that I get from the ajax request, I'm attaching a jQ handler using live/livequery. The problem occurs when I put a checkbox type input that gets through ajax let's say, another input type=radio with 2 options. When I select one of the radio options, instead of sending to the server the correct parent ID, it sends the checkbox id thus returning me the same input radio set.

The jQuery code (wrapped in document ready):

$(function(){
            $('.input_camp :input').live('change',function(){
                $camp = $(this);
                type = $camp.get(0).tagName;
                referinta = '';
                switch(type){
                    case 'SELECT':
                        referinta = $(this).find("option:selected").val();
                    break;
                    case 'INPUT':
                        type2 = $camp.attr('type');
                        if(type2 == 'radio'){
                            referinta = $(this+":checked").val();
                        }else if(type2 == 'checkbox'){
                            var allVals = [];
                            $(this+':checked').each(function(){
                                allVals.push($(this).val());
                            });
                            referinta = allVals;
                        }
                    break;
                }
                $.ajax({
                    type: "POST",
                    url: "ajax.php?actiune=dependente",
                    data: "referinta="+referinta,
                    success: function(response){
                        $camp.parent('div.input_camp').next('div.dependente').html(response);
                    }
                }); 
            }); 
        });

This is the start code for the form:

<div class="camp">
            <div class="label_camp">
                <label for="camp26">judet: </label>
            </div>
            <div class="input_camp">
        <input type="checkbox" class="radio " id="camp3" value="12" name="camp3">
        <label for="camp3">j.1</label><br>
        <input type="checkbox" class="radio " id="camp3" value="13" name="camp3" checked="checked">
        <label for="camp3">j.2</label><br>
            </div>
            <div style="" class="dependente"></div>
        </div>

As I said, after checking the second checkbox, ajax returns the following code inside the div.dependente tags:

ajax request: "ajax.php?referinta=13".

ajax response:

<div class="camp">
            <div class="label_camp">
                <label for="camp27">localitate: </label>
            </div>
            <div class="input_camp">
        <input type="radio" class="radio " id="camp4" value="16" name="camp4">
        <label for="camp4">L.2.1</label>
        <input type="radio" class="radio " id="camp4" value="17" name="camp4" checked="checked">
        <label for="camp4">L.2.2</label>
        <input type="radio" class="radio " id="camp4" value="18" name="camp4">
        <label for="camp4">L.2.3</label>
            </div>
            <div style="" class="dependente"></div>
        </div>

So far, so good. Next, I need to se开发者_开发百科lect one of the radios available. Regardless of the input chosen, the ajax request is the same as before, thus the same response.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜