javascript - 如何使用'onClick'函数获取元素的id
问题描述
我正在尝试使用“onClick”函数捕获整数 id。并用ajax发送它的数据库。并在警报框中显示与此 id 相关的所有信息。
<button class="show">Show Popup</button>
<div class="Popup Confirm">
<input type="text" id="id" />
<button type="button" onclick="getId(document.getElementById('id').innerHTML);">Get</button>
</div>
<script>
$('.show').click(function(){
$(".Popup").show();
$("#id").val(4);
});
function getId(id = null) {
if(id) {
$.ajax({
url: 'abc.php',
type: 'post',
data: {id: id},
dataType: 'text',
success:function(response) {
alert(id);
}
});
}
else {
// error messages
alert("No id selected");
}
}
</script>
解决方案
代替
<button type="button" onclick="getId(document.getElementById('id').innerHTML);">Get</button>
你应该使用:
<button type="button" onclick="getId(document.getElementById('id').value);">Get</button>
输入字段值可通过value
属性获得,而不是innerHTML
.
推荐阅读
- java - 从方法参数中捕获 Java 8 lambda 可变变量?
- laravel - Laravel,VueJS [Vue 警告]:未知的自定义元素:您是否正确注册了组件?
- database - 文件系统与数据库
- python - 如何解释线性回归的可视化
- python - SyntaxError:尝试迁移时语法无效
- javascript - 当原点不同时从 iframe 获取 url
- c++ - 在 MATLAB 到 visualstudio C 环境中缺少 libmx.dll 文件
- c++ - ^() {} 和 ^{} 在 C++ 中是什么意思?
- python - 我有日期,但隐含时间;如何为此熊猫数据框创建日期时间索引?
- java - Android Studio:如何让我的光标也拉出主键?