2012-08-09 53 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上提供基础知识,或者某种类型的东西,以便有人可以为您测试它。

相关问题