简体   繁体   English

如何在vue js html中动态地向我的输入字段提供ID作为我的音译ID的ID?

[英]How to dynamically provide the id to my input field as that of my transliteration id in vue js html?

I am doing a project. 我正在做一个项目。 In one case I need to dynamically add input fields. 在一种情况下,我需要动态添加输入字段。 In that case, for the first input field transliteration is working. 在这种情况下,对于第一个输入字段,音译是有效的。 For the second input field onwards transliteration is not working. 从第二个输入字段开始,音译不起作用。 Because transliteration id works only for one input field. 因为音译ID仅适用于一个输入字段。 So, how can I able to change id of input field and transliteration id dynamically? 因此,如何动态更改输入字段的ID和音译ID? I am beginner in javascript and I am not able to sort out the problem. 我是javascript的初学者,无法解决问题。 Please help me. 请帮我。

So, the code for dynamically adding input fields is 因此,用于动态添加输入字段的代码是

   <div class="row" v-for="(book, index) in seziure" :key="index">


    <div class="col-md-4">
        <div class="form-group label-floating">
            <label class="control-label">Date </label>
            <input type="date" class="form-control" v-model="book.date" >
        </div>
    </div>
    <div class="col-md-8">
        <div class="form-group label-floating">
            <label class="control-label"> Details</label>
            <input type="text" class="form-control" v-model="book.details" id="transliterateTextarea2">
        </div>
    </div>

</div>
<a @click="addNewRow">Add Another </a>

So, whenever I clicks Add Another @addNewRow I am getting a new input field but transliteration is not working. 因此,每当我单击添加另一个@addNewRow时,我都会得到一个新的输入字段,但是音译不起作用。 I think problem arises because id="transliterateTextarea2" works for only one input field. 我认为出现问题是因为id =“ transliterateTextarea2”仅适用于一个输入字段。

So, when I click on @addNewRow how can I able to change the transliteration id. 因此,当我单击@addNewRow时,如何更改音译ID。

My script is 我的剧本是

addForm = new Vue({
el: "#addForm",
  data: {
          seziure:[
          {
             date: null,
             details: null,

          },
        ],
    },
methods: {
      addNewRow: function() {
          this.seziure.push({ date: null, details: null, });
        },
},
})

My transliteration script is 我的音译脚本是

 <script type="text/javascript">

      // Load the Google Transliterate API
      google.load("elements", "1", {
            packages: "transliteration"
          });

      function onLoad() {
        var options = {
            sourceLanguage:
                google.elements.transliteration.LanguageCode.ENGLISH,
            destinationLanguage:
                [google.elements.transliteration.LanguageCode.MALAYALAM],
            shortcutKey: 'ctrl+g',
            transliterationEnabled: true
        };

        // Create an instance on TransliterationControl with the required
        // options.
        var control =
            new google.elements.transliteration.TransliterationControl(options);

        // Enable transliteration in the textbox with id
        // 'transliterateTextarea'.
        var ids = [ "transliterateTextarea2" ];
        control.makeTransliteratable(ids);
      }
      google.setOnLoadCallback(onLoad);
    </script>

So, here I have added a transliteration id as var ids = [ "transliterateTextarea2" ]; 因此,在这里我添加了音译ID作为var ids = [ "transliterateTextarea2" ]; THIS ID is actually working for first input field only. 该ID实际上仅适用于第一个输入字段。 So, when I click @addNewRow, an input field comes but transliteration is not working 因此,当我单击@addNewRow时,输入字段出现,但音译不起作用

So, when I click on@addNewRow how can I dynamically add id's. 因此,当我单击on @ addNewRow时,如何动态添加ID。 So, that I can get transliteration for that input fields too. 因此,我也可以为该输入字段获得音译。

Please help me to have an answer. 请帮我一个答案。

I need transliteration works for, each new input fields i am adding. 我需要为每个要添加的新输入字段进行音译工作。

Kindly have a help on the same. 请在相同的帮助。 Hoping for a help 希望有所帮助

First thing, you are creating non-unique ids, which is just wrong in html. 首先,您要创建非唯一ID,这在html中是错误的。 You should use :id="'transliterateTextarea_' + index" . 您应该使用:id="'transliterateTextarea_' + index" But also your initialization script for transliteration will not register the new input element without rerunning it each time you add an element. 但是,用于音译的初始化脚本也不会在每次添加元素时不重新运行它而注册新的输入元素。 Turn your whole transliteration script into a function and in your addNewRow add something like this: 将整个音译脚本变成一个函数,然后在addNewRow添加如下内容:

this.seziure.push({ date: null, details: null, });
this.$nextTick(()=>{yourTransliterationInitializingFunction(this.seziure.length)})

And in yourTransliterationInitializingFunction make sure to use the proper id (that's why we pass the parameter there): 并在您的yourTransliterationInitializingFunction确保使用正确的ID(这就是我们在此处传递参数的原因):

function yourTransliterationInitializingFunction(idNo){
  ...
  var ids = [ "transliterateTextarea_" + idNo ];
  ...
}

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

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