简体   繁体   English

提交前使用 jquery 更改隐藏字段值

[英]Change hidden field value using jquery before submitting

I am working on a form where there is a hidden input field for button clicked, originally, the value is this field is an empty string.我正在处理一个表单,其中有一个用于单击按钮的隐藏输入字段,最初,该字段的值是一个空字符串。 I want to be able to pass in a value to the hidden field as soon as a button is clicked(so I can know the button that was clicked) before the ajax call starts.在 ajax 调用开始之前,我希望能够在单击按钮后立即将值传递给隐藏字段(这样我就可以知道被单击的按钮)。 Right now, this is what I've tried but the userType field still comes back empty after the ajax call is completed.现在,这是我尝试过的,但在 ajax 调用完成后,userType 字段仍然为空。 Here's my HTML code.这是我的 HTML 代码。

<form role="form"
  id="UserReg"
  name="UserReg"
  >
  
  
<input type="hidden"
       name="user_type_id"
       id="input_userType"
       value="">
       
    <button id="user1" name="user1"> User 1 </button>
    
    <button id="user2" name="user2"> User 2 </button>
               
       

Here's my jQuery code这是我的 jQuery 代码

$("#user1").on('click', function (e)
{   

    //I'm trying to assign 3 to the value of the hidden field before the AJAX call

    $("#input_userType").val("3")

    
    e.preventDefault();

    let Form         =  $('#UserReg');
    



        $.ajax({

            url         :   window.location.protocol + "//" + window.location.host + '/secure/register',
            type        :   'POST',
            contentType :   "application/x-www-form-urlencoded; charset=utf-8",
            cache       :   false,
            processData :   true,
            data        :   Form.serialize()
            success     :   function (returnedData)
            {
                consoleLog('returnedData');
                             
                if(jsonData.status === false)
                {
                    // Display Error Message

                }
                else
                {
                   //Do something
                }
            },
            error       :   function (returnedData)
            {
                  //Do something
            }
        });
    }
});

After the AJAX call, the value of the hidden field still returns an empty string.在 AJAX 调用之后,隐藏字段的值仍然返回一个空字符串。

When modifying the value, you should use the val method.修改值时,应使用val方法。

memberTypeId.val("3");
$("#input_userType").val("3");

or或者

let memberTypeId = $("#input_userType");
memberTypeId.val("3");

https://api.jquery.com/val/ https://api.jquery.com/val/

$("#input_userType").val("3") $("#input_userType").val("3")

i dont know if this could be the cause of the error though,but this is what i Observed from the code you posted我不知道这是否可能是错误的原因,但这是我从您发布的代码中观察到的

$("#user1").on('click', function (e)

{   

//I'm trying to assign 3 to the value of the hidden field before the AJAX call

$("#input_userType").val("3")


e.preventDefault();

let Form  =  $('#UserReg');


    $.ajax({

        url         :   window.location.protocol + "//" + window.location.host + '/secure/register',
        type        :   'POST',
        contentType :   "application/x-www-form-urlencoded; charset=utf-8",
        cache       :   false,
        processData :   true,
        data        :   Form.serialize() // there should be a comma here also
        success     :   function (returnedData)
        {
            consoleLog('returnedData');
                         
            if(jsonData.status === false)
            {
                // Display Error Message

            }
            else
            {
               //Do something
            }
        },
        error       :   function (returnedData)
        {
              //Do something
        }
    });
// this "}" is an excess  one, as i cant locate the beginning. so it either you remove this 
}
});// or this one

So i would suggest you look through your code and adjust appropriately.所以我建议你查看你的代码并进行适当的调整。 then we can pick it up from there.然后我们可以从那里取走它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM