简体   繁体   English

jquery遍历元素并将事件绑定到每个元素上

[英]jquery iterating over elements and binding an event onto each element

I need to bind a keypress event to a form of elements that is built up dynamically, see below 我需要将一个按键事件绑定到一个动态构建的元素形式,见下文

<div class="Customer">
<select name="dropdown1" id="dropdown1">
</select>
<input type="textbox" name="firstname1" id="firstname1">
<input type="textbox" name="lastname1" id="lastname1">
</div>

<div class="Customer">
<select name="dropdown2" id="dropdown2">
</select>
<input type="textbox" name="firstname2" id="firstname2">
<input type="textbox" name="lastname2" id="lastname2">
</div>

... and repeat..

I need to check that whenever any of the elements above are changed that no other of the elements are empty. 我需要检查每当上面的任何元素被更改时,没有其他元素是空的。

The form also has several other elements in it although I only care about these highlighted in the code segment. 该表单中还包含其他几个元素,尽管我只关心代码段中突出显示的这些元素。

Many thanks, 非常感谢,

I've tried the following code 我试过以下代码

$('.Customer').each(function (index) {

    alert("test");  // alerts the correct number of customer rows

    $("input:textbox").click(function() {
        alert("");
    })

});

solution should work with jQuery 1.3.2 解决方案应该与jQuery 1.3.2一起使用

See it working here 看到它在这里工作

<input type="checkbox" id="test" name="test" disabled="disabled">

<div class="Customer">
<select name="dropdown1" id="dropdown1">
    <option value="1.1">1.1</option>
    <option value="1.2">1.2</option>
</select>
<input type="text" name="firstname1" id="firstname1">
<input type="text" name="lastname1" id="lastname1">
</div>

<div class="Customer">
<select name="dropdown2" id="dropdown2">
    <option value="2.1">2.1</option>
    <option value="2.2">2.2</option>
</select>
<input type="text" name="firstname2" id="firstname2">
<input type="text" name="lastname2" id="lastname2">
</div>​

$('.Customer input, .Customer select').bind("change keyup", function(){
     var me = $(this);
     var clicked = me.attr("name");
     var empty = false;
     $('.Customer input,select').each(function(){
         var elem = $(this);
             if (elem.val() == "") {
                 empty = true;
             }
     });
     if (empty) {
        $('#test').attr('disabled','disabled')
     } else {
        $('#test').removeAttr('disabled')
     }
});​

PS: there's no type textbos in input. PS:输入中没有类型的文本框。 It's text. 这是文字。 In order to get a callback fired when something changed you use the "change" and not the "click" event. 为了在更改内容时触发回调,请使用“更改”而不是“单击”事件。 If you want it to work on both the inputs ans the dropdowns, you need "input, select" 如果您希望它同时适用于输入和下拉菜单,则需要“输入,选择”

If you want to attach event handlers on each of those text input you should do 如果要在每个文本输入上附加事件处理程序,您应该这样做

HTML ( type is "text" not "textbox" ) HTML(类型是“文本”而不​​是“文本框”)

<div class="Customer">
<select name="dropdown1" id="dropdown1">
</select>
<input type="text" name="firstname1" id="firstname1">
<input type="text" name="lastname1" id="lastname1">
</div>

<div class="Customer">
<select name="dropdown2" id="dropdown2">
</select>
<input type="text" name="firstname2" id="firstname2">
<input type="text" name="lastname2" id="lastname2">
</div>

js JS

$('.Customer').each(function (index) {
        $("input:text", this).click(function() {
        alert("");
    })

});​

​http://jsfiddle.net/pSFj3/ http://jsfiddle.net/pSFj3/

EDIT - i wanted to point out that you were using a wrong syntax, but as many point out there are bettere ways for handling events. 编辑 - 我想指出你使用了错误的语法,但是很多人指出有更好的方法来处理事件。 for example you could use event delegation 例如,您可以使用事件委派

$('.Customer').on( 'click', 'input:text', function() {
   alert('clicked on input');
});

如果要将click事件附加到“Customer”类的元素中的每个文本框,则不应该这样做:

$(".Customer input[type=text]").click(function() { });

如果在客户端上动态构建表单,则需要使用$.on()函数。

$(document).on('keypress', '.Customer input[type="text"]', function() { ... }); 

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

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