Javascript 将JS函数转换为jQuery-(输入字段清除默认值)
我想知道是否有jQuery专家愿意将下面的脚本转换为jQuery。我自己在转换它时遇到了困难,我更喜欢使用jQuery等价物 我试图做的只是从Submit上的关键字字段中删除默认值“Search”,因为用户可以将关键字字段留空Javascript 将JS函数转换为jQuery-(输入字段清除默认值),javascript,jquery,forms,onsubmit,Javascript,Jquery,Forms,Onsubmit,我想知道是否有jQuery专家愿意将下面的脚本转换为jQuery。我自己在转换它时遇到了困难,我更喜欢使用jQuery等价物 我试图做的只是从Submit上的关键字字段中删除默认值“Search”,因为用户可以将关键字字段留空 function clearValue() { var searchValue = document.getElementById("global-search").value; if (searchValue == "Search") {
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()
相同
以下是与此答案相关的文档:
:.on()
:.val()
:文档准备就绪
- jQuery选择器:
.live()
。从jQuery 1.7开始,它就贬值了,其功能与使用.delegate()
相同。是的,这是.live()的缺点之一:)
if($("#global-search").val() == "Search")
$("#global-search").val("");
function clearValue() {
if ($("#global-search").val() == "Search") {
$("#global-search").val('');
}
}