使用jquery识别最接近的文本输入,没有唯一的id

Identifying closest text input without a unique id using jquery

本文关键字:唯一 id 文本 jquery 识别 最接近 使用 输入      更新时间:2023-09-26

我有一个下拉列表旁边的文本框与加减按钮(http://bootsnipp.com/snippets/featured/buttons-minus-and-plus-in-input),但我有一些困难,使加减按钮只改变它旁边的文本框。

我尝试了各种各样的东西,如。parent(),。close()等,但似乎无法得到任何地方。我认为下面的帖子是有用的,但再次没有得到很远:jQuery parent().find() problem

下面是代码(为样式道歉):http://jsfiddle.net/528AW/69/

我只是想让增加和减少按钮影响它旁边的输入。任何帮助都将非常感激。我想也许就在这附近?

    $( document ).ready(function() {
            $('.btn-number').click(function(e){
                e.preventDefault();
                fieldName = $(this).attr('data-field');
                type      = $(this).attr('data-type');
                var input = $("input[name='"+fieldName+"']");
                var currentVal = parseInt(input.val());
            // around here perhaps

变化

var input = $("input[name='"+fieldName+"']");

 var input = $(this).parent().parent().find("input[name='"+fieldName+"']");

请看下面的例子