How to create input name field with string-variable schema in Vue Js?

前端 未结 2 567
陌清茗
陌清茗 2021-02-08 00:50

I use VueJs, i need to extract javascript variable to generate hidden fields.

But i need to set the name by the index of the variable.

I want to use zig-zag nami

相关标签:
2条回答
  • 2021-02-08 01:07

    To create input elements with dynamic names by index, you can use the + in a JS expression to concatenate:

    <div v-for="(a,index) in test_template" class="row">            
      <input type="hidden" :name="'segment[' + index + '][nb]'" :value="a.nb">
    </div>
    

    Generates:

    <input type="hidden" name="section[0][nb]" value="2">
    <input type="hidden" name="section[1][nb]" value="1">
    <input type="hidden" name="section[2][nb]" value="4">
    

    See: https://vuejs.org/v2/guide/syntax.html#Using-JavaScript-Expressions

    0 讨论(0)
  • 2021-02-08 01:12

    I ran across the same problem as you, and here is how i fixed it !

    make a method like this in your vue-instance

    getInputName(index, dataName){
          return "items["+index+"]["+dataName+"]";
        }    
    

    then you can use it like this:

    <input v-bind:name="getInputName(index, 'name')" type="text" v-model="item.name">
    <input v-bind:name="getInputName(index, 'price')" type="text" v-model="item.price">
    

    which will give you this post result:

    "items" =>[
        0 =>[
          "name" => "test"
          "price" => "23"
        ],
        1 =>[
          "name" => "jakke"
          "price" => "99,2312"
        ]
    ]
    

    And thats it...

    Cheers, Sipman

    0 讨论(0)
提交回复
热议问题