我想在点击id="add_city"按钮时激活该功能截至目前,它激活时,按下Enter ..
function createCity(stateId, newCity, event) {
if(event.keyCode == 13){
if (newCity.trim().length) {
$.ajax({
type:'POST',
url:'ajaxData.php',
data:{'id': id,
'country_id': $("#country").val(),
'state_id': stateId,
'city': newCity},
success:function(html){
$('head').append(html);
$('#state').change();
}
});
}
}
}
定义一个变量并为它设置一个false值然后当按钮点击设置它的值为true然后在主函数中,如果该变量为true,则运行其余代码如下所示:
var isActive=false;
function buttonclicked()
{
isActive=true;
}
function createCity(stateId, newCity, event)
{
if (isActive)
{
//rest of code
}
}
为按钮添加一个事件:
var button = document.getElementById("add_city");
button.addEventListener("click", yourFunction);
请记住在执行该代码之前等待页面加载:
document.onload = function () { your code }