bootstrap-modal:彈出modal時input的checked的狀態無法改變 | | input獲取焦點不生效

1.每次彈出modal時 ,用 js 修改的 checked 狀態不能實時更新

<div class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
          <input type='checked' id='platNumber' >
          <span for='platNumber'>訂單創建日期</span>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
$('#platNumber').attr('checked',true); // 這是一個後臺寫的,他找了好多辦法都是針對modal的,但不奏效

這其實不是 modal 的問題,是 input 的問題

$('#platNumber').prop('checked',true);

checkboxradioselect 這樣的元素,選中屬性對應“checked”和“selected”,這些也屬於固有屬性,因此需要使用 prop 方法去操作才能獲得正確的結果。

2. 在 modal 彈出時,讓 input 獲取焦點

$('#platNumber').focus(); // 並不生效,因爲執行時的 modal 也許還沒渲染完,所以需要定時器

setTimeout(function(){
    $('#platNumber').focus(); // 這就可以生效了
},300);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章