根据我的代码片段,由于 ID 不存在,我在 TypeError 中遇到错误,按钮 ID 可以在管理员中设置为yes、no, 禁用,
$(document).ready(function(){
document.getElementById("no").remove();
document.getElementById("disabled").disabled = true;
document.getElementById('yes').setAttribute('id','<?php echo $update->update;?>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--<button id="<?php echo $update->install;?>">Install</button>-->
<button id="disabled">Install</button>
我的问题:
1) ID不存在时如何解决TypeError?
2) 如何显示 <?php echo $update->update;?>
的值在 JavaScript 中?
最佳答案
由于您使用的是 jQuery
, 只需用它来修复 TypeError
问题。 jQuery
已经进行了检查。
您可以 '<?php echo $update->update;?>'
至 echo
在 js 上。
$(document).ready(function(){
$( "#no" ).remove();
$( "#no" ).attr('id','done');
$( '#disabled' ).prop('disabled', '<?php echo $update->update;?>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--<button id="<?php echo $update->install;?>">Install</button>-->
<button id="disabled">Install</button>
注意:如果您尝试设置按钮的初始状态,建议将其设置为php
。而不是 jQuery
.
$status = "disabled";
echo '<button type="button" ' . $status . '>Click Me!</button>';
关于javascript - 如何使用 Javascript 将按钮 ID 更改为另一个 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48525840/