应该在文本框中使用哪个事件来调用javascript函数?

时间:2013-09-05 14:07:56

标签: javascript html

我有两个文本框来存储两个日期。我想在第一个和第二个文本框中输入日期时调用javascript函数。那就是我在第二个框中输入日期,我想调用javascript函数。我试过onchange但它没有调用javascript函数。你能建议我调用javascript函数的正确事件吗?提前致谢。我用得很聪明。我的代码如下:

<label>Created Date From</label>
            <div class="form-element">
              <input type="text" class="cal fl-left" name="from_date" id="frmDate" value="{if $data.from_date}{$data.from_date}{else}{/if}" maxlength="10" />
            </div>
<label>Created Date To</label>
              <div class="form-element">
                <input type="text" class="cal fl-left" name="to_date" id="toDate" value="{if $to_date}{$to_date}{else}{/if}" maxlength="10" onchange="get_tests_by_date(); return false;"/>
              </div>

Javascript代码如下:

function get_tests_by_date() {
    document.location.href = "view_tests.php?test_category_id="+document.getElementById('test_category_id').value+"&test_mode="+document.getElementById('test_mode').value+"&test_type="+document.getElementById('test_type').value+"&package_type="+document.getElementById('package_type').value+"&created_date_from="+document.getElementById('frmDate').value+"&created_date_to="+document.getElementById('toDate').value+"&page=1";
  }

1 个答案:

答案 0 :(得分:2)

这就是我多年前做的事情,在这段代码中没有jquery:

<强> HTML:

<input name="FormStartDate" value="" maxlength="10" onkeydown="KeyDateNumCheck()"/>
<input name="FormEndDate" value="" maxlength="10" onkeydown="KeyDateNumCheck()"/>

<强> JS:

//do not allow to input anything except the numeric characters, dashes, or slashes
function KeyDateNumCheck()
{
  var iKeyCode = event.keyCode;
  if ( (iKeyCode != 109) && (iKeyCode != 189) && (iKeyCode != 111) && (iKeyCode != 191) &&
    ( ((iKeyCode > 57)&&(iKeyCode < 96)) || (iKeyCode > 105) || (iKeyCode == 32)) )
    event.returnValue = false;
}