सबसे पहले, यह ध्यान रखना महत्वपूर्ण है कि किसी भी रेडियो पर आग पर "क्लिक" घटना पहले से अपडेट की गई है। यह महत्वपूर्ण है - क्योंकि इसका मतलब है कि ईवेंट पहले ही निकाल दिए जाने के बाद आप पिछले आइटम का पता नहीं लगा सकते हैं। यदि आप ईवेंट को रद्द करते हैं, तो आप वास्तव में मूल्य को बदल रहे हैं - शुरुआत में इसे रोक नहीं सकते। यह महत्वपूर्ण है कि आप समस्या से कैसे संपर्क करें।
उदाहरण:
<input type="radio" name="radioButtonGroup" value="button1" checked="true"/>
<input type="radio" name="radioButtonGroup" value="button2"/>
<script type="text/javascript">
var $last = $('[name=radioButtonGroup]:checked');
// Select the radio buttons as a group.
var $radios = $('[name=radioButtonGroup]').bind('change', function (ev) {
// Click event handler
var $clicked = $(ev.target); // This is the radio that just got clicked.
$last.trigger('unclick'); // Fire the "unclick" event on the Last radio.
$last = $('[name=radioButtonGroup]:checked'); // Update the $last item.
// Should see the clicked item's "Value" property.
console.log("Clicked " + $clicked.attr('value'), $clicked, ev);
}).bind('unclick', function (ev) {
// Handler for our new "unclick" event.
// - fires whenever a radio loses focus.
var $unclicked = $(ev.target); // The radio losing it's checked status.
// Should see the unclicked item's "Value" property.
console.log("Unclicked " + $unclicked.attr('value'), $unclicked, ev);
});
</script>
एक काम के लिए:
<input type="radio" name="radioButtonGroup" value="button1" checked="true"/>
<input type="radio" name="radioButtonGroup" value="button2"/>
// At this point, the ':checked' item is button1.
$('input[type=radio]').bind('click', function (ev) {
// If you click on button2 - by this point, the ':checked' item is already button2.
ev.preventDefault(); // These two lines will stop the radio from actually
ev.stopPropagation(); // changing selection.
// At this point, the ':checked' item is set BACK to button1.
});
इस वजह से
, सबसे आसान समाधान के रूप में निम्नानुसार, अपने ईवेंट हैंडलर्स के साथ एक बंद में "पिछले" चयनित आइटम को ट्रैक करने के उदाहरण के लिए, देखें:
http://jsfiddle.net/TroyAlford/wvrtC/
स्रोत
2012-06-13 22:25:08
कृपया सही उत्तर के रूप में एक उत्तर का चयन करें, या अपनी समस्या को हल करने के लिए आपको और जानकारी की आवश्यकता है। –