简体   繁体   English

使用jquery输入的输入的奇怪行为

[英]Strange behavior of the inputs with jquery inputs

I have a form with inputs, the Jquery makes a specific input (the url input) form input to link, but i have for example two inputs of the url, the HTML ids are url0 and url1 and still when i want to edit the first input/url0 the inputs of the url1 is making visible and the changes are made for the url1 not url0 and when click out the input it makes the value of url1 same with the url0, you can see exactly what i mean here - jsfiddle 我有一个带输入的表单,Jquery将一个特定的输入(url输入)表单输入链接,但我有两个url的输入,HTML ids是url0url1仍然当我想编辑第一个输入/ url0 url1的输入是可见的,并且对url1进行了更改而不是url0,当点击输入时,它使url1的值与url0相同,你可以看到我的意思- jsfiddle

thanks a lot 非常感谢

exact steps: 确切步骤:

  1. Form with inputs 表格与输入
  2. two url inputs url0/url1 两个网址输入url0 / url1
  3. onload they are links but with a hidden input in HTML on click activate the input so can be editable onload它们是链接但是在HTML上有隐藏输入,点击激活输入,因此可以编辑
  4. when click out of the input it converts to links again 当单击输出时,它会再次转换为链接

Problem is those two inputs (url0/url1) gets confused and make strange behavior on edit. 问题是这两个输入(url0 / url1)混淆并在编辑时产生奇怪的行为。 please check the link above to see 请查看上面的链接查看

The strange behavour is related to how you are handling your events 奇怪的行为与您处理事件的方式有关

for example 例如

$('#url0,#url1').val($(this).text()).show().focus();

Will open both inputs on a single click on either link. 将在任一链接上单击打开两个输入。

you need to have reference between your input and link and maintain it or change it dynamically from link to input and back. 您需要在输入和链接之间进行引用并对其进行维护或从链接到输入和返回动态更改它。

ps didn't think i would be seeing the exact same code a few days later :P ps几天后我不认为我会看到完全相同的代码:P

See working jsFiddle here . 这里看到工作jsFiddle。

Here is the modified JS which works when you click on the link - 这是修改后的JS,当你点击链接时可以工作 -

$('.a0 a').click(function(e){
    e.preventDefault();
    $(this).parent().parent().find('input').val($(this).text()).show().focus();
    $(this).parent().hide();
})

$('#url0, #url1').each(
    function(index, element){
        $(element).blur(function(){
            $(this).hide().prev().show().find('a').html(this.value);
    })
    }    
);

The following works when you click on the div the link is in - jsFiddle 单击链接所在的div时,以下内容有效 - jsFiddle

$('.a0').click(function(e){
    e.preventDefault();
    $(this).parent().find('input').val($(this).find('a').text()).show().focus();
    $(this).hide();
})

$('#url0, #url1').each(
    function(index, element){
        $(element).blur(function(){
            $(this).hide().prev().show().find('a').html(this.value);
    })
    }    
);

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

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