2012-11-26 28 views
28

Tôi cần xóa thuộc tính "đã chọn" của một hộp kiểm khi xảy ra lỗi.Xóa thuộc tính "đã chọn" của hộp kiểm

Hàm .removeAttr không hoạt động. Bất kỳ ý tưởng? :/

HTML

<div data-role="controlgroup" data-type="horizontal" data-mini="true" style="margin-left: auto; margin-right: auto; width: 100%; text-align: center;"> 
    <input type="checkbox" id="captureImage" name="add_image" class="custom" /> 
    <label for="captureImage" data-icon="checkbox">Image</label> 
    <input type="checkbox" id="captureAudio" name="add_audio" class="custom" /> 
    <label for="captureAudio" data-icon="checkbox">Audio</label> 
    <input type="checkbox" id="captureVideo" name="add_video" class="custom" /> 
    <label for="captureVideo" data-icon="checkbox">Video</label> 
</div> 

Javascript

$("#captureImage").live("change", function() { 
    // $("#captureImage").prop('checked', false); // Here Work 

    if($("#captureImage:checked").val() !== undefined) { 
      navigator.device.capture.captureImage(function(mediaFiles) { 
      console.log("works"); 
     }, function(exception) { 
      $("#captureImage").prop('checked', false); // Not Works Here 
      _callback.error(exception); 
     }, {limit: 1}); 
    } 
}); 

/* 
$("#captureAudio").live("change", function() { 
    if($("#captureAudio:checked").val() !== undefined) { 
      navigator.device.capture.captureAudio(function(mediaFiles) { 
      console.log("audio"); 
     }, function() { 
      $("#captureAudio").removeAttr('checked'); 
      _callback.error; 
     }, {limit: 1}); 
    } 
}); 

$("#captureVideo").live("change", function() { 
    if($("#captureVideo:checked").val() !== undefined) { 
      navigator.device.capture.captureVideo(function(mediaFiles) { 
      console.log("video"); 
     }, function(exception) { 
      $("#captureVideo").prop('checked', false); 
      _callback.error(exception); 
     }, {limit: 1}); 
    } 
}); 
*/ 
+0

$ ("# captureImage: kiểm tra"). Val() sẽ luôn luôn cung cấp cho bạn val hoặc tắt giá trị thậm chí bạn chưa xác định cho nó –

+0

@rajeshkakawat Không, $ val() khác với $ ("# captureImage: checked"). val() – FabianoLothor

+0

Vâng, khác, nhưng các công trình của nó có điều kiện. – FabianoLothor

Trả lời

-5

Xin lỗi, tôi giải quyết vấn đề của tôi với đoạn code trên:

$("#captureImage").live("change", function() { 
    if($("#captureImage:checked").val() !== undefined) { 
     navigator.device.capture.captureImage(function(mediaFiles) { 
      console.log("works"); 
     }, function(exception) { 
      $("#captureImage").removeAttr('checked').checkboxradio('refresh'); 
      _callback.error(exception); 
     }, {}); 
    } 
}); 
58

Hãy thử ...

$("#captureAudio").prop('checked', false); 
+0

Không hoạt động trong gọi lại chức năng lỗi chỉ hoạt động chức năng captureImage. – FabianoLothor

1

Hãy thử:

$("#captureAudio")[0].checked = false; 
+0

Không hoạt động trong chức năng gọi lạiError:/ – FabianoLothor

+0

Tôi đoán bạn đang làm sai điều kiện. Hãy thử: 'if ($ (" # captureImage ") [0] .checked)'. Tránh sử dụng các bộ chọn giả jQuery (': checked'). – macool

+0

@FabianoLothor cũng tránh tăng ngoại lệ. Ngay cả một cái gì đó như 'console.error' có thể làm hỏng mã của bạn. (Điều đó đã xảy ra với tôi) – macool

0

thử một cái gì đó như thế này FIDDLE

try 
     { 
     navigator.device.capture.captureImage(function(mediaFiles) { 
     console.log("works"); 
     }); 
     } 

    catch(err) 
     { 
     alert('hi'); 
     $("#captureImage").prop('checked', false); 

     } 
+0

Tôi thử ở đây, nhưng, không nhập cảnh khi bắt. :/ – FabianoLothor

5

Hãy thử này để kiểm tra

$('#captureImage').attr("checked",true).checkboxradio("refresh"); 

và bỏ chọn

$('#captureImage').attr("checked",false).checkboxradio("refresh"); 
35

Cả hai thứ này sẽ hoạt động:

$("#captureImage").prop('checked', false); 

VÀ/HOẶC

$("#captureImage").removeAttr('checked'); 

... bạn có thể thử cả hai cùng lúc.

+0

Chỉ có tùy chọn thứ hai làm việc cho tôi nhưng tôi không biết tại sao. Có phải vì tôi đặt thuộc tính đã chọn với phương thức attr, như: radio.attr ('checked', 'checked')? –

+0

@ ErikČerpnjak có rồi – itsazzad

+1

** NOTE ** trên [.prop ('checked', false) hoặc .removeAttr ('checked')?] (Https://stackoverflow.com/q/6169826/1366033), rằng * * '.prop (" checked ", false)' ** luôn thích hợp hơn với '.removeAttr (" checked ")' – KyleMit

0

Bạn có thể thử $(this):

$("#captureAudio").live("change", function() { 
    if($(this).val() !== undefined) { /* IF THIS VALUE IS NOT UNDEFINED */ 
      navigator.device.capture.captureAudio(function(mediaFiles) { 
      console.log("audio"); 
     }, function() { 
      $(this).removeAttr('checked'); /* REMOVE checked ATTRIBUTE; */ 
      /* YOU CAN USE `$(this).prop("checked", false);` ALSO */ 
      _callback.error; 
     }, {limit: 1}); 
    } 
}); 
0

sử dụng .removeAttr() trên một thuộc tính boolean như checked, selected, hoặc readonly cũng sẽ thiết lập tương ứng với tên tài sản cho false.

Do đó loại bỏ này kiểm tra thuộc tính

$("#IdName option:checked").removeAttr("checked"); 
Các vấn đề liên quan