0

我有以下 jquery,它在调用函数时添加了一个文本框。在我使用提交按钮提交此文本框之前,我想在提交 onclick 中调用一个 javascript 函数,以确保在提交之前文本框不为空。我该怎么做我不确定如何使用该元素,因为它是动态添加的。

  $("<center><div><input type = \'submit\' onclick=\'\'  name = \'upload\' maxlength = 30/></div></center>").insertAfter("#"+innerid);

  $("<center><div><input type = \'text\' id = \'newteam\' name = \'newteam\' maxlength = 30/></div></center>").insertAfter("#"+innerid);

任何想法我怎么能做到这一点?

4

2 回答 2

2
$('body').on('click', ':submit[name="upload"]', function(e) {
  if(!$.trim($(':input[name="newteam"]').val()).length) {
    alert('empty');
    e.preventDefault();
  }
});
于 2012-04-16T04:57:06.030 回答
0

包装文本框的表单有一个onsubmit带有函数的属性。如果此函数返回 false,则取消提交过程。

看看http://www.w3schools.com/jsref/event_form_onsubmit.asphttp://api.jquery.com/submit/

于 2012-04-16T04:49:33.000 回答