2013-08-07 3 views
16

कृपया मैं, bellow अक्षम/जब एक चेकबॉक्स चेक या अनियंत्रित है बटन को सक्षम करने के लिए एक स्क्रिप्ट है कि एचटीएमएल कोड के साथ काम कर सकते हैं की जरूरत हैअक्षम कैसे करें/एक चेकबॉक्स अगर जाँच के साथ एक बटन को सक्षम

<input type="submit" name="sendNewSms" class="inputButton" id="sendNewSms" value=" Send " />

कृपया दोस्तों का मतलब यह नहीं है कि चेक किए जाने पर बटन अक्षम किया जाए, बल्कि दूसरी तरफ गोल करें।

उत्तर

5

brbcoding मुझे उचित कोडिंग मैं जरूरत के साथ मदद करने में सक्षम है, यहाँ यह

एचटीएमएल

<input type="checkbox" id="checkme"/> 
    <input type="submit" name="sendNewSms" class="inputButton" disabled="disabled" id="sendNewSms" value=" Send " /> 

जावास्क्रिप्ट

var checker = document.getElementById('checkme'); 
var sendbtn = document.getElementById('sendNewSms'); 
// when unchecked or checked, run the function 
checker.onchange = function(){ 
if(this.checked){ 
    sendbtn.disabled = false; 
} else { 
    sendbtn.disabled = true; 
} 

} 
+0

यह अक्षम है और enbaling नहीं है – SAR

26

आप चेकबॉक्स का onchange घटना का उपयोग कर सकते checked मूल्य के आधार पर/अक्षम बटन को सक्षम करने के

<input type="submit" name="sendNewSms" class="inputButton" id="sendNewSms" value=" Send " /> 

<input type="checkbox" onchange="document.getElementById('sendNewSms').disabled = !this.checked;" /> 
+0

धन्यवाद है, लेकिन मेरा दूसरा मतलब कभी नहीं है, उपरोक्त कोड चेक किए जाने पर बटन अक्षम कर दिया गया है, लेकिन मैं चाहता हूं कि बटन अक्षम हो और फिर चेकबॉक्स चेक होने पर सक्षम हो – Chidi

+0

@Chidi ने कोड अपडेट किया। आपको '! '-' not' ऑपरेटर –

+0

जोड़ने के लिए सरल की आवश्यकता है – Rishabh

14

एचटीएमएल

<input type="checkbox" id="checkme"/><input type="submit" name="sendNewSms" class="inputButton" id="sendNewSms" value=" Send " /> 

जे एस

var checker = document.getElementById('checkme'); 
var sendbtn = document.getElementById('sendNewSms'); 
checker.onchange = function() { 
    sendbtn.disabled = !!this.checked; 
}; 

DEMO

+1

प्रदान किए गए अन्य समाधानों की तुलना में यह बेहतर तरीके से काम करता है। जाहिर है कि' sendbtn.disabled = false' को 'sendbtn.disabled = true' या इसके विपरीत आवश्यकतानुसार स्विच करें। अगर आप इसे पहली बार विज़िट करना चाहते हैं, तो आप बस शुरू करने के लिए बटन में अक्षम जोड़ सकते हैं। – brbcoding

+0

धन्यवाद दोस्त, यह अभी ठीक काम करता है, मदद के लिए धन्यवाद। – Chidi

+0

ध्यान रखें, आपको यह सर्वरसाइड भी जांचना होगा। यह सब लेता है devtools में [इस लाइन] (http://i.imgur.com/m6n4vUl.png) से 'अक्षम' को हटा रहा है और बटन फिर से सक्षम है। – brbcoding

9

आपको ऐसा करने के लिए जावास्क्रिप्ट, या JQuery ढांचे का उपयोग करना होगा। उसके Jquery

का उपयोग कर एक उदाहरण है
$('#toggle').click(function() { 
     //check if checkbox is checked 
     if ($(this).is(':checked')) { 

      $('#sendNewSms').removeAttr('disabled'); //enable input 

     } else { 
      $('#sendNewSms').attr('disabled', true); //disable input 
     } 
    }); 

डेमो:

<input type="submit" name="sendNewSms" class="inputButton" id="sendNewSms" value=" Send " /> 
<input type="checkbox" id="disableBtn" /> 

var submit = document.getElementById('sendNewSms'), 
    checkbox = document.getElementById('disableBtn'), 
    disableSubmit = function(e) { 
     submit.disabled = this.checked 
    }; 

checkbox.addEventListener('change', disableSubmit); 

यहाँ कार्रवाई में इसके बारे में एक बेला है:http://jsfiddle.net/T6hvz/

1

मैं jQuery का उपयोग करने की सलाह देता हूं क्योंकि यह आपके लिए भारी भारोत्तोलन करेगा। कोड काफी मामूली है।

$('input:checkbox').click(function() { 
    if ($(this).is(':checked')) { 
    $('#sendNewSms').click(function() { 
     return false; 
    }); 
    } else { 
    $('#sendNewSms').unbind('click'); 
    } 
}); 

चाल 'क्लिक' ईवेंट को ओवरराइड करना और प्रभावी रूप से इसे अक्षम करना है। आप इसे "अक्षम" बनाने के लिए कुछ सीएसएस जादू के साथ भी इसका पालन कर सकते हैं। यदि आपको इसकी आवश्यकता हो तो जावास्क्रिप्ट में कोड यहां दिया गया है। यह सही नहीं है लेकिन यह बिंदु पार हो जाता है।

var clickEvent = function() { 
    return false; 
}; 
document.getElementById('#checkbox').onclick(function() { 
    if (document.getElementById('#checkbox').checked) { 
    document 
     .getElementById('#sendNewSms') 
     .onclick(clickEvent); 
    } else { 
    document 
     .getElementById('#sendNewSms') 
     .removeEventListener('click', clickEvent, false); 
    } 
}); 
संबंधित मुद्दे