![](/img/trans.png)
[英]Dynamically checking two checkboxes to select one radio button using javascript
[英]Unable to select dynamically generated radio button programmatically using javascript
我正在使用 PHP 和 HTML 為預訂頁面動態生成表單元素。 在點擊
注意 - 如果我使用的是 javascript,那么我可以在控制台中看到一個錯誤,但在 jQuery 的情況下沒有錯誤。 下面是我的代碼塊
<div class="time-slot"> <ul class="clearfix"> <?php $counter = 0; for ($x = 0; $x < count($doctorslots_morning); $x++) { $counter++; foreach ($bookings as $booking) {?> <li id="mornig_<?= $counter?>" onclick="ckeckRadiobutton(this.id);"> <a class="timing" href="javascript:void(0);"> <input type="radio" id="radio_morning_<?= $counter?>" name="slot" value="<?= $doctorslots_morning[$x]?>"/> <span><?= $doctorslots_morning[$x]?></span> </a> </li> <?php }?> </ul> </div>
function ckeckRadiobutton(listid) { var radiobuttonid = 'radio_' + listid; console.log(listid); console.log(radiobuttonid ); document.getElementById(radiobuttonid ).checked = true; }
booking:539 Uncaught TypeError: Cannot set properties of null (setting 'checked') at ckeckRadiobutton (booking:539:49) at HTMLLIElement.<anonymous> (booking:528:7) at HTMLLIElement.dispatch (jquery.min.js:2:42571) at v.handle (jquery.min.js:2:40572)
不知道為什么我會收到此錯誤。 我也嘗試過 jQuery 文檔加載方法。 這也有相同的 output。 任何人都可以在這里幫助我解決這個問題。
先感謝您。
我在命名約定中犯了一個錯誤。 感謝@A.Pallis 指出錯誤。
您的單選按鈕 ID 具有前綴radio_morning_
,但您的 javascript 查詢使用radio_
。 那是問題嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.