跳出字段导致" undefined"如果留空

时间:2016-09-08 13:35:27

标签: javascript jquery html

我有一个调用Javascript / JQuery函数的金额字段,所以如果我在该字段中并按下标签而不输入金额,我会在我的字段中显示Undefined

我尝试了以下删除它,但它不起作用,我不知道我能做什么

$( "#ApproximateValue_TextBox" ).keydown(function(){
    alert("ApproximateValue_TextBox");
    document.getElementById("ApproximateValue_TextBox").value = "";
});

$( "#UncryAmount_TextBox" ).keydown(function(){
    alert("UncryAmount_TextBox");
    document.getElementById("UncryAmount_TextBox").value = "No";
});
<tr id="OrigoUncryAmountField">
     <td><label id="AmountUnCryLabel" for="">Approximate amount</label></td>
     <td style="padding-top: 6px; padding-right: 0px; padding-left: 0px;">£</td>
     <td style="padding-right: 0px; padding-left: 0px;">
          <input name="UncryAmount_TextBox" id="UncryAmount_TextBox" onkeyup="SubButtonToDisplay();" onblur="FormatCurrency(this)" type="any" value="UncryAmount_TextBox">
     </td>
</tr>

1 个答案:

答案 0 :(得分:1)

如果使用jquery绑定事件,那么为什么不使用jquery选择元素并利用jquery的其他优点呢?

&#13;
&#13;
$( "#ApproximateValue_TextBox" ).keydown(function(e){
    e.preventDefault();
    $(this).val("");
});

$( "#UncryAmount_TextBox" ).keydown(function(e){
    e.preventDefault();
    $(this).val("No");
});

function FormatCurrency(obj)
{
  // your some code here
}
function SubButtonToDisplay(obj)
{
// your some code here
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<table>
  <tr id="OrigoUncryAmountField">
     <td><label id="AmountUnCryLabel" for="">Approximate amount</label></td>
     <td style="padding-top: 6px; padding-right: 0px; padding-left: 0px;">£</td>
     <td style="padding-right: 0px; padding-left: 0px;">
          <input name="UncryAmount_TextBox" id="UncryAmount_TextBox" onkeyup="SubButtonToDisplay();" onblur="FormatCurrency(this)" type="any" value="UncryAmount_TextBox">
     </td>
</tr>
  </table>
&#13;
&#13;
&#13;