這也許是非常基本的jQuery語法,但我對它不是很熟悉。我只想在頁面上存在具有特定標識的html輸入時才進行jquery ajax調用。我該怎麼做呢?有條件的ajax調用
2
A
回答
2
-
你的腳本標記後包括
- 製作舒爾驗證(例如在關閉
</body
-tag之前)或等待文檔準備好DOM事件。 - 檢查DOM元素的存在。
$(document).ready(function() {
if ($('#jepp').length) { // 0 validates falsy and != 0 truthy
$('#jepp').val('do your stuff eg ajax')
}
});
// or a little bit more advanced
$(function() { // document ready
if (document.getElementById('jepp')) { // vanilla JavaScript but still straight forward
$('#jepp').val($('#jepp').val() + ' | a bit more advanced')
}
})
<input id="jepp" value="" style="width:100%">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
1
您可以檢查是否存在頁面上用id的元素,使用的selector.If長度是,執行您的請求
if($("#yourId").length>0){
//Your request
$.post("ajax/test.html", function(data) {
});
}
+0
長度將爲零它不存在。 – ste2425
2
<input name="foo" id="data">
<script type="javascript">
...
var elementSize = $('#data').size();
if(size > 0){
$.ajax({
...
});
// Page contains id
}else {
//Page doesn't contains id
}
...
</script>
你可以做到這樣。
$('#element-id').size()
或$('#element-id').length
是id元素的尺寸。 Id值必須是1並且是唯一的。你檢查大小,並讓阿賈克斯呼籲
相關問題
- 1. JQuery的條件Ajax調用
- 2. 的Javascript條件AJAX回調
- 3. 有條件調用函數有條件地調用函數
- 4. 有條件地將選項jQuery的Ajax調用
- 5. Django - ajax調用條件方法
- 6. 有條件的jQuery插件調用
- 7. Ajax XMLHttpRequest.responseText有條件工作
- 8. 有條件的微調器
- 9. 模糊調用帶有sizeof條件的enable_if的方法條件
- 10. AJAX調用後條件,事件處理,綁定不起作用
- 11. 調試一個有條件的鏈條
- 12. 有條件的異步調用
- 13. 有條件的JS調用IE7?
- 14. 是有條件使用的回調?
- 15. Thymeleaf有條件的JavaScript函數調用
- 16. 製作JQuery ajax請求有條件的
- 17. 帶鏈接AJAX調用的條件邏輯
- 18. 通過AJAX調用檢索Cookie的條件
- 19. 使用jQuery AJAX調用防止「競爭條件」
- 20. 條碼掃描器和jquery ajax調用?
- 21. 有條件地調用函數
- 22. 有條件地調用JavaScript函數
- 23. 有條件地從bean調用JavaScript
- 24. Luabind有條件地調用Lua函數
- 25. 有條件地調用模板xslt
- 26. 有條件調用觸發器
- 27. 有條件地在C++中調用JNI
- 28. 有條件地並行調用
- 29. 有條件調用鏈接方法
- 30. 有條件地調度副作用
[truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)和 [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy ) – Axel