2013-05-13 59 views
-1

基本上我有50行的表,每行有2个TD。一个用于输入,另一个用于标签,一切都是动态的。如何在包含50个文本框的表中获取对当前文本框的引用?

请参阅该图像 enter image description here

困境 用户在文本框中输入一个数字(我怎么知道哪一个,如果我不想遍历所有的人?)和回车键的事件我调用一个javascript函数,检查它是否有效并将相应的消息添加到下一个tds标签。 如何知道没有循环所有文本框的输入引用,因为我在每个文本框输入的输入函数上调用该函数?

+1

表格是在HTML文件中创建的,还是用JS或其他东西动态生成的?它是什么?你允许使用jQuery吗? – 2013-05-13 18:38:08

+0

不是表格不是动态创建的,但是tit内容是用于ex错误信息的,我可以使用jquery – inputError 2013-05-13 18:39:11

+0

如果以下答案都不足以满足您的要求,那么您可能需要详细询问。 – Mindwin 2013-05-13 20:01:33

回答

1

以下链接的作品的形式,基于(有限的可用信息),但需要你处理验证自己,明明:

function yourDefinedFunction(){ 
    // you need to handle the validating yourself, somehow. 
    return false; 
} 

/* binds the 'keypress' to the 'tr' elements, which 'listens' to see 
    if they take place on/in a 'td' element */ 
$('tbody tr').on('keypress', 'td', function(e){ 
    /* 'this' is the 'td' element, 
     'e.target' is the element within the 'td' that received the event 
    var cell = this, 
     target = e.target; 

    // if the key pressed is the enter key, *and* the target was the input, then: 
    if (e.which === 13 && target.tagName.toLowerCase() == 'input') { 

     // you determine whether the entry is valid, however you want: 
     var validity = yourDefinedFunction() || 'valid'; 

     // and set the text of the next 'td' element to reflect that validity: 
     $(cell).next('td').text(validity); 
    } 
}); 

JS Fiddle demo

参考文献:

+0

正是我想要的。非常感谢 – inputError 2013-05-13 19:52:15

+0

你的确非常欢迎,我很高兴得到了帮助!并感谢*您*接受! = d – 2013-05-13 19:53:55

0

很难给不知道具体你要怎样做一个很好的例子,但像这样的工作:

$('tr > td:first > input').keyup(function(e) { 
    //jQuery object representing the input box the user typed into 
    var input_box = $(this); 

    //get which key was pressed 
    var key_pressed = e.which; 
}); 
0

使用“改变”事件的TextBox,如:先加class='myTextboxClass'添加到您的文本框标记。

$(mytableselector).on('change','.myTextboxClass', function(){ 
    var me = this; // the one that changed 
    $(this).val();//value of that textbox 
}); 

设置同级文本:

$(mytableselector).on('change','.myTextboxClass', function(){ 
    var me = this; // the one that changed 
    $(this).val();//value of that textbox 
    $(me).parent('td').next('td').html('Invalid entry'); 
}); 
0
$("selector for table").on("keyup", "input[type=\"text\"]", function(ev) { 
    if (ev.which !== 13) { 
     return; 
    } 

    alert($(this).value()); 
}); 

Example

+0

没有必要为'input [type = \“text \”]'解释器转义,你已经被包装在一个字符串中了,所以你只需要做'input [type = text]' – Ohgodwhy 2013-05-13 18:45:15

+1

[_Attribute values in选择器表达式必须遵循W3C CSS选择器的规则;一般来说,这意味着除有效标识符以外的任何内容都应该用引号括起来._](http://api.jquery.com/category/selectors/attribute-selectors/) – Andreas 2013-05-13 18:48:35

+0

@Ohgodwhy您说得对,值不**必须用引号括起来,但CSS标准是包含它们 - 'name =“value”'。这是真的没有必要,但它是正确的,我也这样做 – Ian 2013-05-13 18:51:29

0

如果使用上......事件,这将指向当前的输入。 实施例:

<td> 
    <textarea onchange="foo(this)"> 
</td> 
<td> 
    Hello 
</td> 
... 
function (input) { 
    input.parentNode.nextSibling.innerHTML = input.value; 
} 

这个例子将在TD的值更改为textarea的权利textarea的值。 (不是很健壮的代码,但它只是一个例子。)

0
$("#tableID :text").keyup(function(e) { 
    if (e.which !== 13) { //check if the key pressed is Enter 
     return;   // if it is not, stop execution of code 
    } 
    var nextTD = $(this).closest('td').next(); 
    //your code goes after this line 
}); 

nextTD的声明后,你可以插入你的代码,你喜欢更新内容TD。

PS:我假设你的表是在例如下面

JSFiddle Example