[英]Keyup alternative for page load
I have the following JS but the last bit only runs on keyup
, I want this to run on page load, please can you help solve this as I am sure its only a quick fix -我有以下 JS,但最后一点只在
keyup
上运行,我希望它在页面加载时运行,请你帮忙解决这个问题,因为我确信它只是一个快速修复 -
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
jQuery.ajax({
url: "https://geoip-db.com/jsonp",
jsonpCallback: "callback",
dataType: "jsonp",
success: function (location) {
var country = location.country_name;
var region = location.state;
var city = location.city;
var lat = location.latitude;
var long = location.longitude;
// add classes to display location info on page
//City
jQuery(".cityText").text(city);
//Region
jQuery(".regionText").text(region);
//Country
jQuery(".countryText").text(country);
$("#countryfield").val(country);
}
});
$(document).ready(function () {
$("#countryfield").keyup(function () {
$("#lp-pom-button-13").css("display", this.value == "United Kingdom" ? "block" : "none");
});
});
</script>
You can trigger the keyup
event manually after adding the event handler.添加事件处理程序后,您可以手动触发
keyup
事件。
$("#countryfield").keyup(function () {
$("#lp-pom-button-13").css("display", this.value == "United Kingdom" ? "block" : "none");
}).keyup();
Do you want to execute that line ONLY on page load or on page load AND key up?您是否只想在页面加载或页面加载和键上执行该行? What the "page load" means?
“页面加载”是什么意思? When the HTML document loads or when the HTTP (AJAX) response receives?
当 HTML 文档加载或收到 HTTP (AJAX) 响应时?
I Think you need below code that changes CSS when HTTP response receives or use change it manually:我认为您需要以下代码来更改 CSS 当 HTTP 响应接收或使用手动更改它:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
function fixCss()
{
$("#lp-pom-button-13").css("display", this.value == "United Kingdom" ? "block" : "none");
}
jQuery.ajax({
url: "https://geoip-db.com/jsonp",
jsonpCallback: "callback",
dataType: "jsonp",
success: function( location ) {
var country = location.country_name;
var region = location.state;
var city = location.city;
var lat = location.latitude;
var long = location.longitude;
// add classes to display location info on page
//City
jQuery(".cityText").text(city);
//Region
jQuery(".regionText").text(region);
//Country
jQuery(".countryText").text(country);
$("#countryfield").val(country);
fixCss();
}
});
$(document).ready(function () {
$("#countryfield").keyup(function () {
fixCss();
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.