2013-02-26 30 views
5

में मुझे यकीन नहीं है कि मैंने उपरोक्त शीर्षक वाले सभी को भ्रमित कर दिया है। मेरी समस्या इस प्रकार है।किसी चयनित विकल्प के लिए केवल टेक्स्ट बदलते समय जब यह चयनित मोड

मैं अपने कोड के लिए मानक जावास्क्रिप्ट (कोई jQuery) और HTML का उपयोग कर रहा हूं। आवश्यकता यह है कि <select>...</select> मेनू के लिए, मेरे पास अलग-अलग लंबाई की गतिशील सूची है।

अब option[selectedIndex].text > 43 अक्षरों की लंबाई, मैं option[selectecIndex] को एक नए पाठ में बदलना चाहता हूं।

मैं

this.options[this.selectedIndex].text = "changed text"; 
onChange घटना है जो ठीक काम करता है में

को फोन करके यह करने के लिए कर रहा हूँ। एक बार जब उपयोगकर्ता चयन को बदलने का फैसला करता है, तो ड्रॉपडाउन सूची बदले गए पाठ के साथ व्यापक-चयनित-पाठ दिखा रही है। इसे मूल सूची दिखाने की जरूरत है।

मैं स्टंप हो गया हूँ! क्या ऐसा करने का एक आसान तरीका है?

कोई भी मदद महान होगी।

धन्यवाद

+4

आप एक jsfiddle की स्थापना कर सकते हैं? – Supplement

उत्तर

3

आप कुछ डेटा विशेषता में पिछले पाठ मान संग्रहीत और उसका उपयोग वापस जब आवश्यक पाठ रीसेट करने के लिए कर सकते हैं:

document.getElementById('test').onchange = function() { 

    var option = this.options[this.selectedIndex]; 

    option.setAttribute('data-text', option.text); 
    option.text = "changed text"; 

    // Reset texts for all other options but current 
    for (var i = this.options.length; i--;) { 
     if (i == this.selectedIndex) continue; 
     var text = this.options[i].getAttribute('data-text'); 
     if (text) this.options[i].text = text; 
    } 
}; 

http://jsfiddle.net/kb7CW/

+0

यह मेरे जैसा पहले से काम करता है। जब मैं चुनता हूं तो 'विकल्प 1' कहता है, जैसे ही मैं इसे चुनता हूं, यह "परिवर्तित टेक्स्ट" में बदल जाता है और इसे चयनित के रूप में दिखाया जाता है। अब जब मैं ड्रॉप डाउन पर क्लिक करता हूं, तो 'विकल्प 1' अब 'बदले गए टेक्स्ट' के रूप में आ रहा है। मैं हमेशा मूल सूची के रूप में ड्रॉप डाउन विकल्प दिखाना चाहता हूं! – curioussam

2

आप jQuery के साथ सुंदर बस यह कर सकते हैं। http://jsfiddle.net/kb7CW/1/

यहाँ है इसके लिए स्क्रिप्ट:: यहाँ एक काम बेला है

 //check if the changed text option exists, if so, hide it 
$("select").on('click', function(){ 
    if($('option#changed').length > 0) 
    { 
     $("#changed").hide() 
    } 
}); 
//bind on change 
$("select").on('change', function(){ 
    var val = $(":selected").val(); //get the value of the selected item 
    var text = $(':selected').html(); //get the text inside the option tag 
    $(":selected").removeAttr('selected'); //remove the selected item from the selectedIndex 
    if($("#changed").length <1) //if the changed option doesn't exist, create a new option with the text you want it to have (perhaps substring 43 would be right 
      $(this).append('<option id="changed" value =' + val + ' selected="selected">Changed Text</option>'); 
    else 
     $('#changed').val(val) //if it already exists, change its value 

    $(this).prop('selectedIndex', $("#changed").prop('index')); //set the changed text option to selected; 

}); 
+0

समस्या यह है कि मैं jQuery का उपयोग नहीं कर सकता हूं। इस मुद्दे को संभालने का एक मानक जावास्क्रिप्ट तरीका ढूंढ रहा था। – curioussam

+0

आप मानक जावास्क्रिप्ट को आसानी से कनवर्ट कर सकते हैं। यह jquery के बिना डीओएम तत्वों को डालने के लिए सिर्फ एक दर्द है। – Mike

+0

मैं कोशिश करूंगा। मैं एक jQuery पृष्ठभूमि से नहीं आया हूं इसलिए मुझे समझने और बदलने के लिए कुछ समय लगेगा। वैसे भी धन्यवाद! :) – curioussam

संबंधित मुद्दे