ورود

View Full Version : نمایش متن های انتخاب شده در select با استفاده از javascript



maryam.eng
چهارشنبه 18 شهریور 1394, 09:32 صبح
سلام

من ی select نوشتم میخواسم هر بار که روی یکی از گزینه ها کلیک میکنم مقدار آن گزینه در یک متغییر ریخته بشه البته به شرطی که اولین گزینه از select نباشه

اینم کد


<select id="select1" name="listOfSubject" onclick="f1();" >

<c:forEach var="row" items="${result.rows}">
<option ><c:out value="${row.subject_note}"/> </option>
</c:forEach>

</select>


اینم کد جاوا اسکریپ:
function f1(){
var e = document.getElementById("select1");
if(e.options[e.selectedIndex]>=1){
var str = e.options[e.selectedIndex].text;
document.getElementById("bottom5").value = str;
alert(str);
}

arenaw
چهارشنبه 18 شهریور 1394, 17:03 عصر
سوالی نپرسیدید؟!
شما باید توی خط سوم جاوااسکریپت، به جای مقدار آپشن انتخاب شده، فقط ایندکس آپشن انتخاب شده رو چک کنید. اینطوری:

if( e.selectedIndex > 0 ){

maryam.eng
پنج شنبه 19 شهریور 1394, 10:39 صبح
سوالی نپرسیدید؟!
شما باید توی خط سوم جاوااسکریپت، به جای مقدار آپشن انتخاب شده، فقط ایندکس آپشن انتخاب شده رو چک کنید. اینطوری:

if( e.selectedIndex > 0 ){



من کدو به این شکل تغییر دادم.میخوام که وقتی روی گزینه های select کلیک میکنم به جز گزینه اول یک پیام نمایش داده بشه.ولی اصلا پیام نشون نمیده.


function f1(){

if( e.selectedIndex > 0 ){
var e = document.getElementById("select1");

var str = (String) (e.options[e.selectedIndex].text);
document.getElementById("bottom5").value = str;
alert(str);
}
}

mhfarzin
جمعه 20 شهریور 1394, 17:28 عصر
<select id="select_1">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

<input id="selectValue" type="text" />


<script>
var selectHandler = function(event) {
if (this.selectedIndex > 0) {
var text = document.getElementById('selectValue');
text.value = this[this.selectedIndex].innerText;
alert(text.value);
}
};


var select = document.getElementById('select_1');
select.onchange = selectHandler;
</script>