2017-02-10 59 views
0

我試圖獲取具有「selected」屬性的選項的值,以將其與選定的當前選項進行比較。如何獲得具有所選屬性的選項值

function onChangeOption(opt) { 
 
    var update_value = opt.value; 
 
    var selectedValue = '???'; // get selected attribute \t 
 
    if (update_value != selectedValue) { 
 
    // Do some things  
 
    } 
 
}
<select class="form-control" onchange="onChangeOption(this)"> 
 
    <!-- I wanna got the content of option selected=selected--> 
 
    <option selected="selected" value="1">1</option> 
 
    <option value="2">2</option> 
 
</select>

+2

'opt.value'將*總是*選定的選項,所以我不知道你在這裏試圖做什麼 –

+0

我知道,但如何獲得默認選定的值? – alexis

+1

@alexis在加載時存儲它,或者只是在選擇中獲得第一個選項(如果這將是默認的) –

回答

0

我認爲亞歷克西斯實際上想要更多的東西是這樣的:

function onChangeOption(opt) { 
 
    var update_value = opt.value; 
 
    var options = document.getElementsByTagName("option"); 
 
    if (options[0].getAttribute("selected")=="selected") { 
 
    var selectedValue = options[0].value; 
 
    } else { 
 
    var selectedValue = options[1].value; 
 
    } 
 
    if (update_value != selectedValue) { 
 
    // If the selected option's value is not equal to the value of the option with the attribute "selected", then do... (this way, you can change the attribute to any of the options!) 
 
    console.log(selectedValue); 
 
    } 
 
}
<select class="form-control" onchange="onChangeOption(this)"> 
 
    <option selected="selected" value="1">1</option> 
 
    <option value="2">2</option> 
 
</select>

評價的結果,如果你需要什麼。樂意效勞。

+0

的JavaScript只,它實際上會檢查屬性'selected',不像一些其他的答案。它的工作原理就像你說你願意,你可以在'selected'更改爲任何其他''

+0

這件事情是這樣我在尋找,我要把它適應我的真實的語境,非常感謝 – alexis

+1

@alexis - 如果你需要添加更多的'',只需添加一些更'if's和'else's或者使用'case'代替。另外,嘗試時,你可以總是使用純JavaScript,而不是jQuery的,因爲即使辛苦也很書面方式少的程序員,這對電腦多看書。祝你的項目好運! – user7393973

2

只需添加change事件listener.And獲取選中value.You可以通過以下維持array.Like實現selected價值和changed值之間的比較。

values = []//creates an array 
 

 
select = document.querySelector('#myselect'); 
 
values.unshift(select.value); 
 
//console.log(values); 
 
select.addEventListener('change',function(){ 
 
update_value = this.value; 
 
console.log(this.value); 
 
if (update_value != values[0]) { 
 
    // alert('Not matched'); 
 
console.log('Not matched'); 
 
    } 
 
    else{ 
 
    //alert('Matched'); 
 
    console.log('Matched') 
 
    } 
 
});
<select class="form-control" id="myselect"> 
 
    <option selected="selected" value="1"> 1 </option> 
 
    <option value="2"> 2 </option> 
 
</select>

+0

那麼「selected」的屬性是什麼?如果OP需要或不需要,我沒有理想。 – user7393973

+0

選擇的值將被顯示在'change' event.The屬性'selected'用於最初選擇一個值。 –

1

您可以隨時保存先前選擇的值,如果你想以某種方式訪問​​它們後面:working example

HTML:

<select id="mySelect" class="form-control" onchange="onChangeOption(this)"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
    <option value="4">4</option> 
    <option value="5">5</option> 
</select> 
<p>Previous: <span id="prev"></span></p> 
<p>Current: <span id="curr"></span></p> 

JS:

var selectElem = document.getElementById("mySelect"); 
var prev = document.getElementById("prev"); 
var curr = document.getElementById("curr"); 

var allEverSelected = [ selectElem.value ]; 

selectElem.addEventListener("change", function(evt){ 
    allEverSelected.push(this.value); 
    prev.innerHTML = allEverSelected[allEverSelected.length - 2]; 
    curr.innerHTML = allEverSelected[allEverSelected.length - 1]; 
}); 

要訪問默認值,只得到DOM加載後<select>值。在<option>標籤只存在 selected屬性,使除第一<option>元素內<select>默認選項,即:

<select> 
    <option value="1">1</option> 
    <option selected value="2">2</option> 
</select> 

選擇以上的默認值是2

2

// save initial selected value to a variable 
 
var initSelected = $('.form-control option:selected').val(); 
 

 
$('select').on('change', function() { 
 
    // check if the selected value is the same as the initial one was 
 
    if(this.value == initSelected) { 
 
    console.log('same values'); 
 
    } else { 
 
    console.log('not same values'); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select class="form-control"> 
 
    <option selected="selected" value="1">1</option> 
 
    <option value="2">2</option> 
 
</select>

2

我認爲這是你想要的。嘗試一下。

function onChangeOption(opt) { 
 
    var update_value = opt.value; 
 
    console.log(update_value); 
 
    var selectedValue;// = '???'; // get selected attribute \t 
 
// I think this is the one you want 
 
//If you want to select the HTML element, 
 
    selectedValue=document.querySelector("option[value='"+update_value+"']"); 
 

 
    console.log(selectedValue); 
 
// 
 
    if (update_value != selectedValue) { 
 
    // Do some things  
 
    } 
 
} 
 
//onChangeOption(document.querySelector('form')); 
 
function start(){ 
 
    while(typeof document.querySelector('form')!=typeof {}){} 
 
    onChangeOption(document.querySelector('.form-control')); 
 
}
<body onload="start()"> 
 
<select class="form-control" onchange="onChangeOption(this)"> 
 
    <option selected="selected" value="1">1</option> 
 
    <!-- I wanna got this --> 
 
    <option value="2">2</option> 
 
</select></body>

相關問題