2
我正在使用此java腳本代碼從我的sql中獲取數據。我只想在輸入字段有15個字符時才發送請求到php文件。所以我用這個onkeyup="if(this.value.length>14)"
但它沒有工作。可有一個人請修改我的Java腳本代碼..請只有當輸入到達X字符時才發送請求到PHP文件
我正在使用此java腳本代碼從我的sql中獲取數據。我只想在輸入字段有15個字符時才發送請求到php文件。所以我用這個onkeyup="if(this.value.length>14)"
但它沒有工作。可有一個人請修改我的Java腳本代碼..請只有當輸入到達X字符時才發送請求到PHP文件
你的HTML改成這樣:
<input type="text" name="name" maxlength="15" id="name" onkeyup="check_length(this)" />
並添加此功能到您的javascript:
function check_length(thisObject){
if(thisObject.value.length>14){ajaxFunction(thisObject.value)}
}
編輯:
This happens when you define the functions in the head part and call them, when the document is not yet initialized. Move the script part, where the initialization happens and try it out.
我把你的示例代碼(保存在一個空白的html文件中,並測試它):
<input type="text" name="name" maxlength="15" id="name" onkeyup="check_length(this)" />
<script>
function check_length(thisObject){
if(thisObject.value.length>14){ajaxFunction(thisObject.value)}
}
function ajaxFunction(thisObjectValue)
{
console.log(thisObjectValue);
/// Do things ....
}
</script>
@siamek沒有工作兄弟:(它寵壞了我的腳本功能。 – user2373405
@ user2373405它爲我工作。 **再次複製代碼**,如果它仍然不起作用,請檢查您的控制檯以查看出現了什麼錯誤。 –
錯誤:'[19:31:48.804] ReferenceError:ajaxFunction未定義@ ..........:12'(第12行是'if(thisObject.value.length> 14){ajaxFunction(thisObject .value)}' – user2373405