简体   繁体   中英

Jquery bug on firefox but not on Chrome

I'm trying to load an input box at the place of a div when clicking on that div, the Jquery I'm using works fine on Chrome, but when coming to Firefox it shows different results after 1, 2 and 3 clicks :
在此处输入图片说明

Here is the Jquery I'm using :

$(function(){
  $('#right').on('click', '.stock.mini-counts', function(){
    var $p = $(this);
    var old = $p.html();

    if(/<input type="text"/.test(old))
      return;

    $p.html('<input type="text" value="' + old + '"/>')
       .find('input')
       .focus()
       .on('blur', function(){
         var value = this.value;
         $.post('listener_updates.php', {stock: value})
          .done(function(){
            $p.html(value);
          })
          .fail(function(){
            $p.html(old);
            alert('Could not update title');
          });
       });
  });
});

Html :

<div class="status"><div class="stock mini-counts">7</div><div>available</div></div>

Thank you for your help

It's because firefox add the value attribute before the type attribute so your regexp not match.

Try change your code in:

$(function () {
    $('#right').on('click', '.stock.mini-counts', function(){
        var $p = $(this);
        var old = $p.html();

        if (/<input /.test(old)) return;
        $p.html('<input type="text" value="' + old + '"/>')
            .find('input')
            .focus()
            .on('blur', function () {
            var value = this.value;
            $p.html(value);

        });
    });
});

Demo: http://jsfiddle.net/IrvinDominin/aAAh8/

Why use the test() function for that when you can just replace that with

if( $(this).find('input').length > 0 ) return false;

which will prevent such errors and check if the input is present across browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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