将单个脚本用于多个输入选择器

时间:2017-11-21 02:26:31

标签: javascript jquery html jquery-plugins keyboard

我想更改下面的代码,在多个地方只有一个ID,而不是使用多个ID(keyboard1keyboard2keyboard3等。)

我需要将脚本用于多个输入框。

<div class="col-md-6 mt-20 flip">
    <div class="input-group">
        <input id="search_field" type="text" id="" class="search_field form-control">
        <span class="input-group-addon key-show"><i class="fa fa-keyboard-o"></i></span>
    </div>
    <div id="keyboard" class="keyboard show-allkey"></div>
</div>

<div class="col-md-6 mt-20 flip">
    <div class="input-group">
        <input id="search_field1" type="text" id="" class="search_field form-control">
        <span class="input-group-addon key-show"><i class="fa fa-keyboard-o"></i></span>
    </div>
    <div id="keyboard1" class="keyboard show-allkey"></div>
</div>
<div class="col-md-6 mt-20 flip">
    <div class="input-group">
        <input id="search_field2" type="text" id="" class="search_field form-control">
        <span class="input-group-addon key-show"><i class="fa fa-keyboard-o"></i></span>
    </div>
    <div id="keyboard2" class="keyboard show-allkey"></div>
</div>

$('#keyboard').jkeyboard({      
  layout: "english",
  input: $('#search_field')
});
$('#keyboard1').jkeyboard({     
  layout: "english",
  input: $('#search_field1')
});
$('#keyboard2').jkeyboard({     
  layout: "english",
  input: $('#search_field2')
});

我的演示网址:https://rawgit.com/saravanasksp/jkeyboard/master/index.html

2 个答案:

答案 0 :(得分:1)

lower_right循环中遍历类。这将公开单个实例并允许您遍历以获取关联的搜索字段

each

答案 1 :(得分:0)

这样的事情应该这样做:

$('.keyboard').each(function() {
    $(this).jkeyboard({
        layout: "english",
        input: $(this).prev('.input-group').find('.search_field');
    });
});