简体   繁体   English

即使使用 Jquery,也可以在 onClick 上动态生成输入值

[英]Get dynamically generated input value on an onClick even using Jquery

I am trying to get respective values of dynamically generated inputs.我正在尝试获取动态生成的输入的相应值。 In other words, I have an X number of dynamically generated inputs;换句话说,我有 X 个动态生成的输入; each of these inputs is bound to a button.这些输入中的每一个都绑定到一个按钮。 With that being said, I would like the user to get alerted the dynamically generated input that is bound to the clicked button.话虽如此,我希望用户能够收到绑定到单击按钮的动态生成输入的警报。 What I have done so far does not sort this out and whatever button is clicked, only the first input's value is generated.到目前为止我所做的并没有解决这个问题,无论单击什么按钮,都只会生成第一个输入的值。

I have the following code - a dynamic input and a button:我有以下代码 - 一个动态输入和一个按钮:

<input type="hidden" id="job_id" name="jobIdName" value="{{ job_id }}">  // please note this input is dynamically generated....

<button name="get_id_name"  class="get_id_class" id="get_id_id" >Show Id</button>

As for Jquery, I have done the following:至于Jquery,我做了以下:

$('#get_id_id').each(function(index) {

    $(this).click(function() {
        var job_ids = $("[name='jobIdName']");

        console.log('Job Ids -------------- : ' + job_ids);

    });
});

The above code keeps generating only the first generated input value?上面的代码一直只生成第一个生成的输入值? Any ideas or suggestions?有什么想法或建议吗?

I have seen some posts that might seem similar to this one but they are very old;我看过一些看起来与这篇类似的帖子,但它们已经很老了; also I am looking for a more modern implementation.我也在寻找更现代的实现。

Add your "input tag" into div:将您的“输入标签”添加到 div 中:

 var counter = 0; $(document).ready(function(){ $("#get_id_id").click(function() { var divChildren = $(".job_ids").children(); if(counter < divChildren.length){ if(counter == '0'){ console.log($(divChildren).eq(0).val()); }else{ console.log($(divChildren).eq(counter).val()); } counter++; } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class ="job_ids"> <input type="hidden" name="jobIdName" value="Test01"> <input type="hidden" name="jobIdName" value="Test02"> <input type="hidden" name="jobIdName" value="Test03"> <input type="hidden" name="jobIdName" value="Test04"> <input type="hidden" name="jobIdName" value="Test05"> </div> <button name="get_id_name" class="get_id_class" id="get_id_id" >Show Id</button>

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

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