jQuery

2012-07-19 29 views
19

के साथ अगला विकल्प चुनें मैं एक बटन बनाने की कोशिश कर रहा हूं जो अगला विकल्प चुन सकता है।jQuery

तो, मैं एक का चयन करें (आईडी = selectionChamp) कई विकल्पों के साथ, एक इनपुट अगले (आईडी = fieldNext) है, और मैं ऐसा करने की कोशिश:

$('#fieldNext').click(function() { 
    $('#selectionChamp option:selected', 'select').removeAttr('selected') 
      .next('option').attr('selected', 'selected'); 

    alert($('#selectionChamp option:selected').val());  
}); 

लेकिन मैं अगले विकल्प का चयन नहीं कर सकते .. धन्यवाद !

+0

क्या आप पृष्ठ लोड पर डीओएम लोड कर रहे हैं? – Dev

+0

आपका क्या मतलब है? –

+0

का मतलब है कि आप उस कोड का उपयोग तैयार फ़ंक्शन में कर रहे हैं? – Dev

उत्तर

22
$('#fieldNext').click(function() { 
    $('#selectionChamp option:selected').next().attr('selected', 'selected'); 

    alert($('#selectionChamp').val());  
}); 
+1

एफएफ 32.0.3 – njahnke

+4

में टूटा हुआ वर्तमान विकल्प को अचयनित करने की आवश्यकता है उदा। '$ ('# चयनChamp विकल्प: चयनित')। attr ('चयनित', झूठा);' – anthonygore

+0

हाँ ... लेकिन केवल तभी चुनें जब चुनिंदा टैग पर एकाधिक विशेषता सेट की गई हो। हालांकि, अगर आप चाहें तो उत्तर को अपडेट करने के लिए स्वतंत्र हैं करने के लिए .... – bugwheels94

0
$('#fieldNext').click(function() { 
$('#selectionChamp option:selected').removeAttr('selected') 
     .next('option').attr('selected', 'selected'); 

alert($('#selectionChamp option:selected').val());  
}); 
0

इस प्रयास करें:

$(document).ready(function(){ 
     $("#fieldNext").on("click",function(){ 
      $optionSelected = $("#selectionChamp > option:selected"); 
      $optionSelected.removeAttr("selected"); 
      $optionSelected.next("option").attr("selected","selected");  
     }); 
    }); 
2
$(function(){ 
    $('#button').on('click', function(){ 
    var selected_element = $('#selectionChamp option:selected'); 
    selected_element.removeAttr('selected'); 
    selected_element.next().attr('selected', 'selected'); 

    $('#selectionChamp').val(selected_element.next().val()); 

    }); 
}); 

http://jsbin.com/ejunoz/2/edit

8

jQuery के बिना भी बहुत आसान है। यह एक इच्छा एक बार पिछले पहला विकल्प के लिए चारों ओर पाश तक पहुँच जाता है:

function nextOpt() { 
    var sel = document.getElementById('selectionChamp'); 
    var i = sel.selectedIndex; 
    sel.options[++i%sel.options.length].selected = true; 
} 

window.onload = function() { 
    document.getElementById('fieldNext').onclick = nextOpt; 
} 

कुछ परीक्षण मार्कअप:

<button id="fieldNext">Select next</button> 
<select id="selectionChamp"> 
<option>0 
<option>1 
<option>2 
</select> 
+0

+1। आपने कुछ अच्छे वेनिला जेएस समाधान पोस्ट किए हैं! बहुत मदद करता है! – A1rPun

0

अन्य समाधान के मामले में काम नहीं करते विकल्प के अलावा तत्वों optiongroup देखते हैं तत्वों। उस मामले में, यह काम करने के लिए लगता है:

var options = $("#selectionChamp option"); 
var i = options.index(options.filter(":selected")); 
if (i >= 0 && i < options.length - 1) { 
    options.eq(i+1).prop("selected", true); 
} 

(आप सोच सकते हैं कि i के लिए अभिव्यक्ति भी options.index(":selected") रूप में लिखा जा सकता है, लेकिन है कि हमेशा काम नहीं करता मैं पता नहीं क्यों, एक विवरण होगा। स्वागत है।)