简体   繁体   English

在javascript / jquery中为输入元素的ID分配变量值

[英]Assigning variable value to id of an input element in javascript/ jquery

I'm trying to assign variable value to dynamically created input elements. 我正在尝试将变量值分配给动态创建的输入元素。 But I couldn't. 但是我不能。 The input elemts are created but not with id. 输入元素已创建,但没有ID。

        $(document).ready(function(){

// Declaring global variables   

    var cnt = 1;
    var fooId = "foo";
    var cnt1 ='aaaa';


// Code for Changing the datalist for second input testingphase when ever first input Tasks has been changed.


        $("#tasks" + cnt).change(function () {
        $("#activitytype" + cnt).replaceWith('<input id="activitytype" + ctn value="asasa" name="ActivityType" placeholder="ActivityType" >');

        if (bla1 == 'Service Request') {
            $("#testingphase" + cnt).replaceWith('<input id="testingphase" + ctn list="Service Request" name="Tasks" placeholder="Tasks2">');
        //  changingactivity ();


             }

You have to change your replaceWith method slightly to include the variable without quotes. 您必须稍微更改replaceWith方法,以包含不带引号的变量。 Change: 更改:

 $("#activitytype" + cnt).replaceWith('<input id="activitytype" + ctn value="asasa" name="ActivityType" placeholder="ActivityType" >');

To something like: 像这样:

 $("#activitytype" + cnt).replaceWith('<input id="activitytype' + ctn + '" value="asasa" name="ActivityType" placeholder="ActivityType" >');

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

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