简体   繁体   English

在jquery中获取当前单击的项目值

[英]Get current clicked item values in jquery

I am showing item and its property list on template using django. 我正在使用django在模板上显示项目及其属性列表。 item have saveral property- name, price, vendor etc. 项目有多个属性 - 名称,价格,供应商等。

i am showing these items by for loop. 我通过for循环显示这些项目。 i am trying to edit item detail for this i need to send new/old values to jquery function. 我正在尝试编辑项目详细信息,我需要将新/旧值发送到jquery函数。

my code looks like- 我的代码看起来像 -

{% for i in item %}
   <div class="form-group">
       <label for="email">Product Id:</label>
       <input type="text" class="form-control" name="productID" id="productID" value={{i.productID}}>
   </div>
   <div class="form-group">
       <label for="email">Product Id:</label>
       <input type="text" class="form-control" name="productName" id="productName" value={{i.productName}}>
   </div>

   <div class="form-group">
       <label for="email">Product Id:</label>
       <input type="text" class="form-control" name="vendor" id="vendor" value={{i.vendor}}>
   </div>
        <button type="submit" id="submit" class="btn btn-default">Submit</button>
{% endfor %}

jquery- jquery-

<script type="text/javascript">
    $('#submit').click(function () {
        var productid = $("#productID").val();
        var vendor = $("#vendor").val();
        var productname = $('#productName').val();
        console.log(productid)
        console.log(vendor)
        console.log(productname)
    });
</script>

But it is not working for me. 但这对我不起作用。 please help. 请帮忙。

thanks in advance 提前致谢

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id

The id global attribute defines a unique identifier (ID) which must be unique in the whole document. id全局属性定义唯一标识符(ID),该标识符在整个文档中必须是唯一的。 Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS). 其目的是在链接(使用片段标识符),脚本或样式(使用CSS)时标识元素。

You can't have multiple elements with the same ID or things will not work as expected. 您不能拥有具有相同ID的多个元素,否则事物将无法按预期工作。

What I would do is get rid of the ID completely, and just wrap each element in some sort of container. 我要做的是完全摆脱ID,并将每个元素包装在某种容器中。 When the submit is clicked, find the .closest container, and use that as the jQuery context to find the inputs with the name's that you care about. 单击提交时,找到.closest容器,并将其用作jQuery 上下文,以查找具有您关注的名称的输入。

 $('.submit').on('click', function() { var $item = $(this).closest('.item'); var productid = $("[name='productID']", $item).val(); var vendor = $("[name='vendor']", $item).val(); var productname = $("[name='productName']", $item).val(); console.log(productid) console.log(vendor) console.log(productname) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> {% for i in item %} <div class="item"> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control" name="productID" value={{i.productID}}> </div> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control" name="productName" value={{i.productName}}> </div> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control" name="vendor" value={{i.vendor}}> </div> <button type="submit" class="submit btn btn-default">Submit</button> </div> {% endfor %} 

The main problem is because you're repeating the same id multiple times in the DOM. 主要问题是因为你在DOM中多次重复相同的id That is invalid HTML. 那是无效的HTML。 To group elements with common behaviours use classes instead. 要使用常见行为对元素进行分组,请使用类。

Then you can use DOM traversal from the clicked button to find the related input elements and read their values by using a combination of closest() and find() . 然后,您可以使用单击button DOM遍历来查找相关的input元素,并使用closest()find()的组合来读取它们的值。 Try this: 尝试这个:

 $('.submit').click(function() { var $container = $(this).closest('.container'); var productid = $container.find(".productID").val(); var vendor = $container.find(".vendor").val(); var productname = $container.find('.productName').val(); console.log(productid) console.log(vendor) console.log(productname) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control productID" name="productID" value="{{i.productID_1}}"> </div> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control productName" name="productName" value="{{i.productName_1}}"> </div> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control vendor" name="vendor" value="{{i.vendor_1}}"> </div> <button type="submit" class="btn btn-default submit">Submit</button> </div> <div class="container"> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control productID" name="productID" value="{{i.productID_2}}"> </div> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control productName" name="productName" value="{{i.productName_2}}"> </div> <div class="form-group"> <label for="email">Product Id:</label> <input type="text" class="form-control vendor" name="vendor" value="{{i.vendor_2}}"> </div> <button type="submit" class="btn btn-default submit">Submit</button> </div> 

Note that this adds one extra wrapping div element within your loop so that it's much easier to retrieve the related elements. 请注意,这会在循环中添加一个额外的包装div元素,以便更容易检索相关元素。

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

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