Javascript 按Enter键时,它会移动到其他页面,而不是调用Java脚本(代码附加)
当我按下txtZip文本框上的enter键时,它会移动到其他页面,而不是调用JS。我想将另一个文本框值eqaul设置为txtZipJavascript 按Enter键时,它会移动到其他页面,而不是调用Java脚本(代码附加),javascript,asp.net,events,textbox,Javascript,Asp.net,Events,Textbox,当我按下txtZip文本框上的enter键时,它会移动到其他页面,而不是调用JS。我想将另一个文本框值eqaul设置为txtZip <asp:TextBox Style="text-align: center" ID="Txt_Zip" runat="server" Width="120px" Text="Zip" onkeypress="return runScript(event)"></asp:TextBox> <asp
<asp:TextBox Style="text-align: center" ID="Txt_Zip" runat="server" Width="120px"
Text="Zip" onkeypress="return runScript(event)"></asp:TextBox>
<asp:TextBox Style="text-align: center" ID="Txt_Second" runat="server" Width="120px"
Text="Zip"></asp:TextBox>
function runScript(e)
{
if (e.which == 13 || e.keyCode == 13)
{
var zip =document.getElementById("txtZip").value;
document.getElementById("txt_Second").value=zip;
}
}
函数运行脚本(e)
{
if(e.which==13 | | e.keyCode==13)
{
var zip=document.getElementById(“txtZip”).value;
document.getElementById(“txt_Second”).value=zip;
}
}
因为我有一个按钮,在点击时会重新指向另一个页面,在文本框中按enter键时如何限制,只需调用JS
希望得到你的建议
谢谢只需添加
e.preventDefault()。这告诉事件不要执行默认操作(提交表单)
试试这个:
function runScript(e)
{
if (e.which == 13 || e.keyCode == 13)
{
var zip =document.getElementById("txtZip").value;
document.getElementById("txt_Second").value=zip;
return false;
}
}
试试这个:
<script>
$(document).ready(function() {
$("input[id$='Txt_Zip']").on('keypress', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) {
$("input[id$='Txt_Second').val($(this).val());
return false;
}
});
});
</script>
$(文档).ready(函数(){
$(“输入[id$='Txt\U Zip']”)。在('keypress',函数(e)上{
var代码=(e.keyCode?e.keyCode:e.which);
如果(代码==13){
$($(输入[id$='Txt_Second').val($(this.val()));
返回false;
}
});
});
e.preventDefault()
在本机JS中不可用,是吗?JQuery在这里没有标记。是的,它是。虽然承认不在IEit中,但它仍在重定向到另一个页面尝试添加警报或控制台。记录以确保事件实际正在触发。它发出警报意味着它的触发事件,但重定向到另一个页面
<script>
$(document).ready(function() {
$("input[id$='Txt_Zip']").on('keypress', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) {
$("input[id$='Txt_Second').val($(this).val());
return false;
}
});
});
</script>