Home > Software engineering >  Jquery Post with dynamic key
Jquery Post with dynamic key

Time:11-26

I would like to send a post request. But I have to change the key.

function call(obj) {
    console.log("Call "   obj.value);
    if (obj.value == 1) 
    {
        $.post( "index.htm", { '"webdata".web[1].Taster': '1'} );
    }

I would like to change [1] to a dynamic different number. To take a car between with do not work.

Do you have any idea? Thank you

Thanks for your help

CodePudding user response:

"webdata".web[1].Taster is a string I will send it to a Siemens plc. For the Javascript it is only a string that I have to change the number inside. Like "webdata".web[2].Taster or "webdata".web[35].Taster but in a dynamic way

CodePudding user response:

This way we set the keyname like : "webdata".web[1].Taster

objX={value:1}


function call(obj) {
    console.log("Call "   obj.value);
    if (obj.value == 1) 
    {
    var postParms = {};
    key='"webdata".web[' obj.value '].Taster';
    console.log(key)
    
    postParms[key] = 1;   //or  ..= obj.value
        $.post( "index.htm",  postParms); 
    }
    
    }

call(objX)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

This way we set Ipsum as a keyname :

objX={value:1}
webdata={
web:[
{Taster:"Lorem"},{Taster:"Ipsum"}
]
}

function call(obj) {
    console.log("Call "   obj.value);
    if (obj.value == 1) 
    {
    var postParms = {};
    key=webdata.web[obj.value].Taster;
    console.log(key)
    postParms[key] = 1;     
        $.post( "index.htm",  postParms); 
    }
    
    }
  
call(objX)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  • Related