繁体   English   中英

为什么我的JavaScript函数不能在所有按钮上正常工作?

[英]Why is my JavaScript function not working on all of my buttons?

我可以在具有不同ID的两个不同元素上调用JavaScript函数吗?

HTML

<p id="id1">12</p>
<button type="button" onClick="ABC(document.getElementById('id1').innerHTML)">one</button>

<input type="text" id="id2" value="21"/>
<button type="button" onClick="ABC(document.getElementById('id2').innerHTML)">two</button>

JS

function ABC(id = null) {
    if(id) {    
        $.ajax({
            url: 'url.php',
            type: 'post',
            data: {id: id},
            dataType: 'json',
            success:function(result) {
            }
        });  
    } 
} 

该功能在第二个按钮上不起作用,我不知道为什么。

第二个按钮使用输入数据,因此您应该使用document.getElementById('id2')。value

 function ABC(id = null) { if(id) { $.ajax({ url: 'url.php', type: 'post', data: {id: id}, dataType: 'json', success:function(result) { } }); } } 
 <p id="id1">12</p> <button type="button" onClick="ABC(document.getElementById('id1').innerHTML)">one</button> <input type="text" id="id2" value="21"/> <button type="button" onClick="ABC(document.getElementById('id2').value)">two</button> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM