2013-10-04 48 views
2

我有以下jQuery代碼在Chrome中工作,但在Firefox中失敗(沒有錯誤,只是不起作用)。當我嘗試使用雙擊鼠標選擇文本框內的文本,而不是選擇文本時,光標移到文本的開頭。任何想法如何解決這個問題?使用jQuery在焦點上選擇文本在Firefox中不工作

我jqueryandajax

$(document).ready(function() { 

    $('td.edit').click(function() { 
     $('.ajax').html($('.ajax input').val()); 
     $('.ajax').removeClass('ajax'); 
     $(this).addClass('ajax'); 
     $(this).html('<input id="editbox" size="' + $(this).text().length + '" type="text" value="' + $(this).text() + '">'); 
     $('#editbox').focus(); 
    }); 

    $('td.edit').keydown(function (event) { 
     arr = $(this).attr('class').split(" "); 
     if (event.which == 13) { 
      $.ajax({ 
       type: "POST", 
       url: "supplierprice/config.php", 
       data: "value=" + $('.ajax input').val() + "&rowid=" + arr[2] + "&field=" + arr[1], 
       success: function (data) { 
        $('.ajax').html($('.ajax input').val()); 
        $('.ajax').removeClass('ajax'); 
       } 
      }); 
     } 
    }); 

    $('#editbox').live('blur', function() { 
     $('.ajax').html($('.ajax input').val()); 
     $('.ajax').removeClass('ajax'); 
    }); 
}); 

我的HTML

<html> 

    <table id="sorting" class="tablesorter" style="width: 100px; table-layout: fixed;" > 

    <thead> 
    <tr> 
    <th>6xxA <span> <img id="logo" src="/image/Picture2.png" style="margin:-62px -21px -9px 17px"></span> 
    <th>6xxB <span> <img id="logo" src="/image/Picture2.png" style="margin:-62px -21px -9px 21px"></span> 
    </th> 
    <th >10xx <span> <img id="logo" src="/image/Picture2.png" style="margin:-62px -21px -9px 32px"></span> 
    </th> 
    <th >11xx <span> <img id="logo" src="/image/Picture2.png" style="margin:-62px -21px -9px 32px"></span> 
    </th> 
    <th >12xx <span> <img id="logo" src="/image/Picture2.png" style="margin:-62px -21px -9px 32px"></span> 
    </th> 
    <th >11xx <span> <img id="logo" src="/image/Picture2.png" style="margin:-62px -21px -9px 32px"></span> 
    </th> 
    </tr> 


    </thead> 


    <tbody > 
    <?php 
    $dbHost = 'localhost'; // usually localhost 
    $dbUsername = 'fms'; 
    $dbPassword = 'xxxxxxxx'; 
    $dbDatabase = 'fms'; 
    $db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server."); 
    mysql_select_db ($dbDatabase, $db) or die ("Could not select database."); 



    $sql = mysql_query("SELECT * FROM supplierprice"); 


    while($rows=mysql_fetch_array($sql)) 
    { 

    if($alt == 1) 
      { 
       echo '<tr class="alt">'; 
       $alt = 0; 
      } 
      else 
      { 
       echo '<tr>'; 
       $alt = 1; 
      } 

    echo ' <td class="edit region '.$rows["supp_price_id"].'">'.$rows["region"].'</td> 
      <td class="edit country '.$rows["supp_price_id"].'">'.$rows["country"].'</td> 
      <td class="edit networkname '.$rows["supp_price_id"].'">'.$rows["networkname"].'</td> 
       <td class="edit mcc '.$rows["supp_price_id"].'">'.$rows["mcc"].'</td>  
       <td class="edit mnc '.$rows["supp_price_id"].'">'.$rows["mnc"].'</td> 
       <td class="edit mnp '.$rows["supp_price_id"].'">'.$rows["mnp"].'</td> 

        </tr>'; 


    } 

    ?> 


    </tbody> 


    </table> 

    </body> 
    </html> 
+0

請張貼全HTML代碼 –

+0

貼出我的代碼認罪se指導如何解決這個問題 – Xavi

+0

[see here](http://stackoverflow.com/questions/7046798/jquery-focus-fails-on-firefox) –

回答

0

請嘗試這可能是這將有助於

 $('td.edit').click(function(e){ 
var $target = $(e.target); 
      if($target.is('#editbox')){ 
       return; 
      } 
             $('.ajax').html($('.ajax input').val()); 
             $('.ajax').removeClass('ajax'); 

             $(this).addClass('ajax'); 
             $(this).html('<input id="editbox" size="'+$(this).text().length+'" type="text" value="' + $(this).text() + '">'); 

             $('#editbox ').focus(); 



            } 




         ); 
相關問題