繁体   English   中英

将JS函数转换为jQuery-(输入字段清除默认值)

[英]Convert JS function to jQuery - (input field clear default value)

我想知道是否有一些jQuery专家会这样将下面的脚本转换为jQuery。 我自己转换它时遇到了麻烦,非常希望使用jQuery等效项。

我正在尝试做的只是从关键字字段onSubmit中删除“搜索”的默认值,因为用户可以将关键字字段留空。

function clearValue() {
    var searchValue = document.getElementById("global-search").value;
    if (searchValue == "Search") {
        document.getElementById("global-search").value = "";
    }
}

任何帮助将不胜感激。

//wait for the DOM to be ready (basically make sure the form is available)
$(function () {

    //bind a `submit` event handler to all `form` elements
    //you can specify an ID with `#some-id` or a class with `.some-class` if you want to only bind to a/some form(s)
    $('form').on('submit', function () {

        //cache the `#global-search` element
        var $search = $('#global-search');

        //see if the `#global-search` element's value is equal to 'Search', if so then set it to a blank string
        if ($search.val() == 'Search') {
            $search.val('');
        }
    });
});

注意.on()是jQuery 1.7中的新功能,在这种情况下与.bind()相同。

以下是与此答案相关的文档:

if($("#global-search").val() == "Search")
    $("#global-search").val("");
function clearValue() {
    if ($("#global-search").val() == "Search") {
        $("#global-search").val('');
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM