T
T
tory_kuzya2017-08-30 11:49:30
JavaScript
tory_kuzya, 2017-08-30 11:49:30

Yii1 Form serrialize() not working, why?

Form submission not working.
Structure:

<form action="#" method="post" class="answer-form form-horizontal form-bordered" id="edit<?=$request->id?>">
                            <div class="clearfix">
                                <!-- Правая колонка заявки-->
                                <div class="col-sm-12 col-md-5 any-tariff">
                                    ...
                                </div>
                                <!-- Левая колонка  -->
                                <div class="col-sm-12 col-md-7 extended-tariff">
                                    ...
                                </div>
                                <!-- Блок, скрывающий контакты от пользователей с базовым тарифом -->
                                <div class="col-sm-12 col-md-7 change-the-tariff <?if(!$rules):?>change-visible<?endif;?>">
                                    ...
                                </div>
                                
                            <div class="form-group answer">
                                <label class="col-sm-12 col-md-8 control-label">
                                    <label class="col-sm-3 control-label">Кол-во машин:</label>
                                    <div class="col-sm-2">
                                        <input type="number" class="form-control" id="trucks_count" min="1" step="1" maxlength="2" value="1" onkeyup="return proverka(this);" onchange="return proverka(this);" name="trucks_count">
                                    </div>
                                    <label class="col-sm-3 control-label">Цена, руб:</label>
                                    <div class="col-sm-2">
                                        <input  type="text" class="form-control" value="<?=$request->price?>" id="price" onkeyup="return proverkaDecimal(this);" onchange="return proverkaDecimal(this);" name="price">
                                    </div>
                                </label>
                                <div class="col-sm-12 col-md-4">
                                    <button style="float: right;" type="button" class="btn btn-success saveRequest" id="saveRequest<?=$request->id?>">Ответить</button>
                                </div>
                            </div>
                        </form>

and there is a whole list of such forms on the page

$('.oldRows').on('click', '.saveRequest', function(){
        $.post("<?=$this->createUrl('/ajax/ansferToRequestShipping');?>",{data:$("#edit<?=$request->id?>").serialize()},function(data){
            $("#status").html(data);
        });
    });

So, the question is how to modify the line:
$.post("<?=$this->createUrl('/ajax/ansferToRequestShipping');?>",{data:$("#edit<?=$request->id?>").serialize()}

So that the values ​​\u200b\u200bof the fields #trucks_count and #price, set by default, are read (the user will not always enter them, maybe the default ones will suit him)?
Can somehow break the form into 2 variables, like:
var a = $('#trucks_count').val();
var b = $('#price').val();

then how to fit them into the existing string? ps: if you read the value value, will there be problems with submitting the form when entering values ​​​​manually into the fields?
{data:$("#edit<?=$request->id?>").serialize()}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cage, 2017-08-30
@tory_kuzya

$.post("<?=$this->createUrl('/ajax/ansferToRequestShipping');?>",$("#edit<?=$request->id?>").serialize()...

default values ​​will be read

M
Maxim Timofeev, 2017-08-30
@webinar

In theory, this data should get there. Check what will happen in the console if you do this:

$('.oldRows').on('click', '.saveRequest', function(){
        var data = $("#edit<?=$request->id?>").serialize();
        console.log(data);
});

T
tory_kuzya, 2017-08-30
@tory_kuzya

The solution is:

$(".saveRequest").on('click',function (){
        var obj = $(this).parent().parent();
        $.post("<?=$this->createUrl('/ajax/ansferToRequestShipping');?>",{data:obj.find('input').serialize()},function(data){
            obj.next('.clearfix').find('h4').html(data);
        });
    });

I noticed that in the dom tree
closes immediately after opening and all nested elements and fields are obtained by neighboring elements. I didn't find the reason.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question