如何使用TextBoxFor检测在ASP.NET中清除类型“搜索”HTML5输入?

时间:2016-07-12 17:35:40

标签: jquery asp.net html5 razor javascript-events

我在X'' X'按下按钮="搜索" textfield action upon pressing X button。  我想使用@Html.TextBoxFor(model => model.SearchTerm, new { id = "search-box", type = "search"}。然而,它不能专门在清除按钮上发射事件' X'单击。

到目前为止,我已设法注册所有活动:

            $('#search-box').on('click', function (e) {
            alert(e);

            System.Diagnostics.Debug.Write(e.target);
        });

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

我尝试过以下代码:

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="clearable" type="text" name="" value="" placeholder="" />

JQuery:

function tog(v){return v?'addClass':'removeClass';} 
$(document).on('input', '.clearable', function(){
    $(this)[tog(this.value)]('x');
}).on('mousemove', '.x', function( e ){
    $(this)[tog(this.offsetWidth-18 < e.clientX-this.getBoundingClientRect().left)]('onX');
}).on('touchstart click', '.onX', function( ev ){
    ev.preventDefault();
    alert('33');// put window.location('url goes here');
    $(this).removeClass('x onX').val('').change();
});

CSS:

.clearable{
  background: #fff url(http://i.stack.imgur.com/mJotv.gif) no-repeat right -10px center;
  border: 1px solid #999;
  padding: 3px 18px 3px 4px;     /* Use the same right padding (18) in jQ! */
  border-radius: 3px;
  transition: background 0.4s;
}
.clearable.x  { background-position: right 5px center; } /* (jQ) Show icon */
.clearable.onX{ cursor: pointer; }              /* (jQ) hover cursor style */
.clearable::-ms-clear {display: none; width:0; height:0;} /* Remove IE default X */

小提琴在这里:click here

希望它可以帮助你.. :)

快乐编码..