Problem to record radiobutton in db with Jquery

Asked

Viewed 26 times

0

I have a form where the data placed on it is recorded in the db by a jQuery, but we radiobutton cannot save the item I select.
Below a part of my form. On the radiobutton Sexo_Segurado that I have the problem.

<div class="col-sm-4 col-sm-offset-2">
    <div class="form-group">
        @Html.LabelFor(model => model.CPF_Segurado, htmlAttributes: new { 
            @class = "Myclass" 
        })
        @Html.EditorFor(model => model.CPF_Segurado, new { htmlAttributes = new { 
            @id = "CPF_Segurado", 
            @type = "text", 
            @placeholder = "Ex: 142.035.046-43", 
            @class = "form-control" 
        } })
        @Html.ValidationMessageFor(model => model.CPF_Segurado, "", new { 
            @class = "text-danger" 
        })

    </div>
</div>
<div class="col-sm-4">
    <div class="form-group">
        @Html.LabelFor(model => model.Data_Nasc_Segurado, htmlAttributes: new { 
            @class = "Myclass" 
        })
        @Html.EditorFor(model => model.Data_Nasc_Segurado, new { htmlAttributes = new { 
            @type = "date", 
            @placeholder = "01/01/2019", 
            @class = "form-control" 
        } })
        @Html.ValidationMessageFor(model => model.Data_Nasc_Segurado, "", new { 
            @class = "text-danger" 
        })
    </div>
</div>
<div class="col-sm-4 col-sm-offset-2">
    <div class="form-group">
        @Html.LabelFor(model => model.Sexo_Segurado, htmlAttributes: new { 
            @class = "Myclass"
         })
        <br />

        @Html.RadioButtonFor(model => model.Sexo_Segurado,"Masculino", new {  
            name = "Masculino", 
            @value = "Masculino", 
            @id = "Sexo_Segurado",  
            @class = "with-gap" 
        })
        <label for="Masculino">Masculino</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        @Html.RadioButtonFor(model => model.Sexo_Segurado, "Feminino", new {  
            name = "Feminino", 
            @value = "Feminino", 
            @id = "Sexo_Segurado", 
            @checked = "checked", 
            @class = "with-gap" 
        })
        <label for="Feminino">Feminino</label>
    </div>
</div>

Below follows my Jquery that I use to save the form data:

function SalvarCliente() {
debugger;
//Segurado
var cpf_segurado = $("#CPF_Segurado").val();
var data_nasc_segurado = $("#Data_Nasc_Segurado").val();
var sexo_segurado = $("#Sexo_Segurado").val();
var token = $('input[name="__RequestVerificationToken"]').val();
var tokenadr = $('form[action="/Cliente/Create"]inputname="__RequestVerification"]').val();

var headers = {};
var headersadr = {};
headers['__RequestVerificationToken'] = token;
headersadr['__RequestVerificationToken'] = tokenadr;

var url = "/Cliente/Create";
$.ajax({
    url: url
    , type: "POST"
    , datatype: "json"
    , headers: headersadr
    , data: {
        Id: 0,
        CPF_Segurado: cpf_segurado,
        Data_Nasc_Segurado: data_nasc_segurado,
        Sexo_Segurado: sexo_segurado,
        __RequestVerificationToken: token
     }
    , success: function (data) {
        if (data.Resultado > 0) {

        }
    }
});

2 answers

0

Guys I think I solved. in this part of Jquery:

var sexo_segurado = $("#Sexo_Segurado").val();

just fix it to:

var sexo_segurado = $("#Sexo_Segurado:checked").val();

that works.

  • Try it like this: $("#Sex Insured"). attr('checked')

0

Now another problem has appeared. I have this Jquery that controls my fieldtooltip:

!function (e) {e('input[type="radio"]').click(function () {
    for (var t = e("input[name='" + e(this).attr("name") + "']"),
        n = 0; n < t.length; n++)e(t[n]).parents(".radio").removeClass("erro"),
            e(e(this).parents("div").get(1)).children(".msg-erro").html(""),
            e(e(this).parents("p").get(1)).children(".msg-erro").html("")
    }),
    e('input[type="radio"]').each(function () {
    e(this).focus(function () {
        e(this).parent().addClass("radioFocus")
    }),
        e(this).blur(function () {
        e(this).parent().removeClass("radioFocus")
    })
    }),e("#mesma-pessoa-fieldtooltip").length > 0 && (e("#Confirm_Proprietario_1").change(function () {
        e(this).is(":checked") && e("#mesma-pessoa-fieldtooltip").hide()
    }),     

        e("#Confirm_Proprietario_0").change(function () {
        e(this).is(":checked") && e("#mesma-pessoa-fieldtooltip").show()
        }),
        e("#Confirm_Proprietario_1").trigger("change"),
        e("#Confirm_Proprietario_0").trigger("change")),}(jQuery);

Oberserve that each radiobutton I use an id, but in Jquery that I use to write to db it only records if it has the same id radiobutton. How could I change the above script so that the fieldtooltip works for radiobutton with same id?

Browser other questions tagged

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