2012-08-09 102 views
0

我有一段時間的屏幕鍵盤工作良好。一個編輯文本字段的按鈕。將jQuery添加到組合中

我最近不得不添加jQuery到系統,現在它不再工作。按下按鈕不會執行任何操作,因爲按下按鈕時文本輸入不再有焦點,而且我無法從jscript重新對焦。

輸入HTML(請注意使用的優秀jq_watermark - 的原因,我需要的jQuery):

<input class="search jq_watermark" id="barcode" name="barcode" type="text" title="Please enter search criteria."/> 

下面是一個按鈕的HTML:

<td onclick="addChar('Q')"><button class="osk" id="Key_Q" value="Q">Q</button></td> 

這裏的addChar腳本。請注意,許多嘗試把重點領域,全部失敗,現在jQuery的存在:

// The field the keyboard should edit. 
field = document.forms['main'].elements.item('barcode'); 

function addChar(c) { 
    console.log("Char("+c+")"); 
    field.focus(); 
    if(field.maxLength == -1 || field.value.length < field.maxLength) { 
    // Handle different browsers. 
    if (field.selectionStart || field.selectionStart == '0') { 
     var start = field.value.substr(0,field.selectionStart); 
     var end = field.value.substr(field.selectionEnd,field.length); 
     field.value = start + c + end; 
    } else if (document.selection) { 
     field.focus(); 
     var range = document.selection.createRange(); 
     range.text = c; 
     range.moveStart('textedit',1); 
     range.select(); 
    } else { 
     field.value = field.value + c; 
    } 
    field.focus(); 
    } 
    return false; 
} 

我不得不添加此停止按鈕提交表單。我懷疑這是我需要把新的東西,但新的jQuery我不知道什麼:

$("button.osk").click(function(event){ 
    // Stop it submitting in case the keyboard is inside a form. 
    event.preventDefault(); 
}); 

p.S.我知道周圍整齊的jQuery彈出式鍵盤,並且很樂意用他們的替換我的,但現在不是一種選擇。

回答

0

問題解決了:

看來這個問題在這裏:

field = document.forms['main'].elements.item('barcode'); 

顯然jQuery的到來使這個不行。

var field; 
$(document).ready(function(){ 
    field = document.forms['main'].elements['barcode']; 
}); 

請注意,我是包裹在一個ready功能,並通過items不訪問:我暫時跟取而代之。這在IE8中不起作用。我將不得不解決這個問題。

0

使用jQuery的時候,你必須把下面之間的所有jQuery腳本:

<script> 
    $(document).ready(function()){ 
    //your jquery stuff here 
    }); 
</script> 

我指出,這是因爲你沒有提供所有的代碼的唯一原因。是否有可能在jsFiddle.net上提供基礎知識,或者某種類型的東西,以便有人可以爲您測試它。

相關問題