简体   繁体   English

如何从跨度和绑定更改事件中选择子div中的上一个输入元素?

[英]How to select previous input element in child div from span and bind change event?

The HTML structure in question looks as follows: 有问题的HTML结构如下所示:

<div class="form-group form-text">
  <div class="input-group">
    <span class="input-group-btn">
      <div id="47" class="btn btn-danger input-xs remove-farming-action">
        <span class="glyphicon glyphicon-remove"></span>
      </div>
    </span>
    <input id="action_CropCount" 
           class="form-control input-xs numeric-text-box crop-counter-textbox" 
           type="number" 
           value="8" 
           name="action.CropCount" 
           min="1" 
           max="999">
  </div>
  Aubergine Violetta lunga 3
  <span class="crop-count-crop-word">crop(s)</span>
  <br>
</div>
(... And so on for different items.)

So this is a list of form-groups which all have an input field where a value can be selected and a span-field with a text in it. 因此,这是一组表单组,所有表单组都有一个输入字段,可以在其中选择一个值,以及一个跨度字段,其中包含一个文本。 What I want to do is change the "crop-count-crop-word" text when the input field with class "crop-counter-textbox" is 1 (from plural to non-plural). 我想做的是,当类别为“ crop-counter-textbox”的输入字段为1(从复数到非复数)时,更改“ crop-count-crop-word”文本。 The challenge is that the span element next to the input box must change, not all others. 挑战在于必须更改输入框旁边的span元素,而不是所有其他元素。

The solution I chose (if there is a better one let me know) is to use JavaScript to select input field and bind a change event to it as follows: 我选择的解决方案(如果有更好的解决方案,请告诉我)是使用JavaScript选择输入字段并将更改事件绑定到它,如下所示:

makeCropPluralWhenCropCountIsBiggerThan1: function() {
    // Every crop count input field needs a span label with crop or crops depending on the count.
    $('.crop-count-crop-word').prev('input').change(function() {
        $(this).next('span').text( $(this).val() == 1 ? 'crop' : 'crops' );
    });
    // Ensure text is correct on load by triggering change event.
    $('.crop-count-crop-word').prev('input').trigger('change');
}

Unfortunately this does not work. 不幸的是,这不起作用。 I do not manage to select the previous input field from the span element. 我无法从span元素中选择上一个输入字段。 No item is selected. 未选择任何项目。

Can anyone tell how to select the "crop-count-crop-word" span which is next from the input with class "crop-counter-textbox"? 谁能说出如何选择“ crop-count-crop-word”跨度,该跨度是类“ crop-counter-textbox”的输入中的下一个?

/Edit here is a Fiddle: https://jsfiddle.net/4ftx1b2b/1/ /编辑这里是一个小提琴: https : //jsfiddle.net/4ftx1b2b/1/

You can select it directly: 您可以直接选择它:

$(".crop-counter-textbox").change(function(e){});

or from your span 或从您的跨度

$('span.crop-count-crop-word').prev(".input-group").find("input.crop-counter-textbox").change(function(e){});

.prev selets at the same level, but at the same level there is a div.input-group and not your input.crop-counter-textbox, that's why your selector does not find it. .prev处于同一级别,但是在同一级别上有一个div.input-group而不是您的input.crop-counter-textbox,这就是为什么选择器找不到它的原因。

You are trying to select previous input element but there is actually a div element with the input inside it. 您正在尝试选择上一个input元素,但实际上有一个div元素,其内部有input So you need to change $('.crop-count-crop-word').prev('input') to $('.crop-count-crop-word').prev('.input-group').find('input') . 因此,您需要将$('.crop-count-crop-word').prev('input')更改$('.crop-count-crop-word').prev('.input-group').find('input')

Edited your code ( fiddle ): 编辑了您的代码( 小提琴 ):

function makeCropPluralWhenCropCountIsBiggerThan1() {
// Every crop count input field needs a span label with crop or crops depending on the count.
$('.form-group').each(function() {
    var $input = $(this).find('input:first');
    var $span = $(this).find('.crop-count-crop-word:first');
    $input.change(function() {
        $span.text( $(this).val() == 1 ? 'crop' : 'crops' );
        $input.trigger('change');
    });        
});

} }

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

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