简体   繁体   English

为什么在我的示例中,两次按下提交按钮才起作用?

[英]Why does the submit button only work when pressed twice on my example?

Please look at this fiddle: 请看这个小提琴:

http://jsfiddle.net/uNbYu/1/ http://jsfiddle.net/uNbYu/1/

When the submit button is pressed once, It removes the text edit for no apparent reason, but doesn't do anything, but the second time, it does work when pressed, and goes to the page. 一次按下提交按钮时,它没有明显的理由删除文本编辑,但是不执行任何操作,但是第二次,按下后它可以工作并转到页面。

Please help me with what makes this code do that. 请帮助我了解这段代码的作用。

Thanks 谢谢


HTML: HTML:

<b class = "edit" id = "fooo"> FOO </b>

JS: JS:

$('b.edit').click(function() {
    $(this).hide().after('<form action = "foo.php" method = post><input hidden name = "field" type = "text" value = "' + this.id + '"/><input type="text" name = "period" class="editP" value="' + $(this).html() + '" /><input type = "submit" value = "Submit!!" /></form>');
    $('.editP').focus();
});
$('.editP').live('blur', function() {
    $(this).hide().prev('b.edit').html($(this).val()).show();
});

This is what your code says: 这就是你的代码说的:

  1. When text is clicked, create a new input element, submit button and form. 单击文本后,创建一个新的输入元素,提交按钮和表单。
  2. On blur hide the input element (not the submit button) 模糊时,隐藏输入元素(而不是提交按钮)

So when you click submit the first time it is actually firing the blur event and hiding the input. 因此,当您第一次单击提交时,它实际上是触发模糊事件并隐藏输入。

Also, your old text is not being reshown because you use .prev to get it. 另外,不会显示旧文本,因为您使用.prev来获取它。 This select previous adjacent sibling, but because the input element is in a form, the other element is not a sibling. 这将选择先前的相邻同级,但是由于输入元素为某种形式,因此其他元素不是同级。

This Fiddle should make what is wrong more obvious. 这个小提琴应该使问题更明显。 Note how the initial alert does not retrieve the text FOO. 请注意初始警报如何不检索文本FOO。 Also try and click submit button. 也尝试单击提交按钮。

http://jsfiddle.net/uNbYu/8/ http://jsfiddle.net/uNbYu/8/

问题在于,在您设法单击按钮之前,编辑框会失去焦点,从而更改了位置,以致您不会单击按钮。

1) When you click the text, your code creates the form and put the focus on .editP . 1)当您单击文本时,您的代码创建表单并将焦点放在.editP

$('.editP').focus();

2) The rest of your code says to hide .editP when it loses focus (blur). 2)您其余的代码说在失去焦点时会隐藏.editP (模糊)。

$('.editP').live('blur', function(){
    $(this).hide().prev('b.edit').html($(this).val()).show();
});

3) So the act of trying to click the button causes the blur event to trigger on editP . 3)因此, 尝试单击按钮的行为导致blur事件在editP上触发。 This hides .editP thus shifting over the button which prevents it from being clicked. 这将隐藏.editP从而将其移至按钮上方,以防止其被单击。 It happens very fast... before you can actually successfully click the button. 它发生得非常快...在您实际上可以成功单击按钮之前。

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

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