Jquery seems not to update

Asked

Viewed 45 times

1

I have a Customer code = 3368. I made a function jquery that takes the code from Dropdown, which may be 1 or 3368 (only has two records there in the dropdown). It turns out that when I have in dropdown 1 the Customer is 3368 and if I change the dropdown to 3368 then the Customer goes to 1. This is the jquery I started to mount

$('#btnSalvar').click(function () {
        var dropdown = $('#faturarPara').val();
        var customerId = @Model.CustomerId;
        alert('Drop2 é: ' + dropdown + ' e customer é: ' + customerId);
    });

What intrigues me about all this is if I run another jquery in the dropdown change call, then it works 1 and 3368 and then 3368 and 3368. But then it doesn’t happen anymore. That’s the change

$(document).ready(function () {
        $('#faturarPara').change(function () {
            var idFaturarDrop = $(this).val();
            var customerId = @Model.CustomerId;

            alert('Customer é: ' + customerId + ' customer options = ' + idFaturarDrop);
        });
    });

I honestly don’t understand. The correct thing would be: In the dropdown I change (de->to) Resale->Final Client(3368 and 3368). From Final Client->Resale(3368 and 1)

This is the Dropdown

@Html.DropDownListFor(m => m.CustomerId, new SelectList(Model.Options, "CustomerId", "Description", Model.CustomerId), new { @id= "faturarPara" })

1 answer

1

Hello, so I think the problem here should be the Onchange Vent that is triggered when you load your dropdown because it considers switching from empty to something and recovers that value, if you have a dropdown think about using the .select() that and if value is the value you want to recover, you can also use

$( "#iddoteuselect option:selected" ).text()

to retrieve text from your options

EDIT

 var customerId = $(this).find('option:selected').attr('CustomerId')

I hope I’ve helped

  • I don’t know if I understand, but what I need to do is compare the two ID’s, which I take in Dropdown(variable) with the model ID(fixed). I don’t know if getting the text would be the right one. I need his ID

  • I need a little more context so I can help. But I think retrieving the value of the selected item would suffice 'var id = $('.Selected-item'). val();' E then you can compare the recovered id

  • I posted the Dropdown code. What I want to do is compare the past Ids.

  • I updated the answer

  • Scrapbench, I’m taking a Undefined on this approach of yours.,

  • Strange should not be recovering the value of select however if perbeci well the attribute is Customerid

  • My problem is not in the dropdown ID, this is correct. The whole question is in the customerId. It is changing and should not, in my view, only the drop ID should change. The other is fixed and still can not understand why

  • By what variable you are transferring the customerId that is causing problem ?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.