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

发布于 2025-01-03 19:52:41 字数 370 浏览 0 评论 0原文

我想知道是否有 jQuery 专家愿意将下面的脚本转换为 jQuery。我自己转换它时遇到了麻烦,并且更喜欢使用 jQuery 等效项。

我想做的只是从提交时的关键字字段中删除“搜索”的默认值,因为用户可以将关键字字段留空。

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

任何帮助将不胜感激。

I was wondering if some jQuery expert would be so kind to convert the below script to jQuery. I am having trouble converting it myself and would much prefer to use a jQuery equivalent.

What I am trying to do is simply remove the default value of 'Search' from a keyword field onSubmit, as the user can leave the keyword field blank.

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

Any help would be much appreciate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小兔几 2025-01-10 19:52:41
//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() 相同。

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

//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('');
        }
    });
});

Note that .on() is new in jQuery 1.7 and in this case is the same as .bind().

Here is the documentation related to this answer:

夏日落 2025-01-10 19:52:41
if($("#global-search").val() == "Search")
    $("#global-search").val("");
if($("#global-search").val() == "Search")
    $("#global-search").val("");
他是夢罘是命 2025-01-10 19:52:41
function clearValue() {
    if ($("#global-search").val() == "Search") {
        $("#global-search").val('');
    }
}
function clearValue() {
    if ($("#global-search").val() == "Search") {
        $("#global-search").val('');
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文