duanrong5167 2016-02-02 14:28
浏览 130
已采纳

在$ .post - JQuery中创建动态变量赋值

Could i use dynamic variable inside my $.post JQuery/Ajax code? I would like to know where i am making mistakes or i am misunderstood the useage of $.post of JQuery.

I have dynamically created variables and value assigned to it from text-fields. Now i want to use those variables inside $.post of JQuery.

Code Below:

The part that's working fine:

var boxFields = ["customerId","customerName","customerContact","customerEmail"];
var boxVar = [];

for(var x=0;x<4;x++)
{
    boxVar[x] = $("#" + boxFields[x]).val();
}

alert(boxVar[1]); //Just random call to check it works.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="customerId" value="12345"/>
<br>
<br>
<input type="text" id="customerName" value="John Doe"/>
<br>
<br>
<input type="text" id="customerContact" value="XXXXXXXXXX"/>
<br>
<br>
<input type="text" id="customerEmail" value="xxxxxx@xxx.com"/>

Now i have dynamically generated variables (check above hidden snippet for that). So to use those in $.post.

Update:

Sorry, for missing the actual part

My Problem/Question: Could i use for loop to create dynamic objects inside $.post.

Whole Code:

var boxFields = ["customerId","customerName","customerContact","customerEmail"];
var boxVar = [];

for(var x=0;x<4;x++)
{
    boxVar[x] = $("#" + boxFields[x]).val();
}

$.post("url.php",
{
  requestType: "updateRow",
  
  for(var y=0;y<4;y++)
  {
    boxFields[y]: boxVar[y] 
    if(y!=3){,}  
  }
  
  /* I want above code to work like this:
  requestType: "updateRow",
  customerId: 12345,
  customerName: John Doe,
  customerContact: XXXXXXXXXX,
  customerEmail: xxxxxx@xxx.com
  */
},
function(data)
{
 //Success Code Lines..........
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="customerId" value="12345"/>
<br>
<br>
<input type="text" id="customerName" value="John Doe"/>
<br>
<br>
<input type="text" id="customerContact" value="XXXXXXXXXX"/>
<br>
<br>
<input type="text" id="customerEmail" value="xxxxxx@xxx.com"/>

Also would like to know how to prevent from getting SQL Injection

Thank You!

</div>
  • 写回答

2条回答 默认 最新

  • doutao5419 2016-02-02 14:52
    关注

    Could i use for loop to create dynamic objects inside $.post.

    Not inside the call to .post(), no. Which has nothing to do with .post() itself or with AJAX or anything like that, but simply as the syntax of the language. Consider your attempt:

    $.post("url.php",
    {
      requestType: "updateRow", 
      for(var y=0;y<4;y++)
      {
        boxFields[y]: boxVar[y] 
        if(y!=3){,}  
      }
      //...
    

    The second argument to the function is simply an object. And things like loops and other control structures aren't valid for declaring an object. What you can do is, well, what you already did before. Build the object, then use it in the function call:

    var someArray = [];
    for(var x = 0; x < 4; x++)
    {
        someArray[x] = someValue;
    }
    // etc.
    
    // later...
    $.post('url.php', someArray, function () { /.../ });
    

    Basically, a loop is not an object that can be passed to a function. It's a control structure for imperative code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)