코드 질문 드립니다 ㅠㅠ jquery change() 관련 ㅠ
본문
<input type="radio" name="seller" value="sell">판매
<input type="radio" name="seller" value="buy">구입
<select id="select">
</select>
<br/>
<br/>
<div id="price_k"></div>
<script>
var fruitlist = new Array();
fruitlist.push({fruit : "사과" , sellprice : 1000 , buyprice : 500});
fruitlist.push({fruit : "멜론" , sellprice : 2000 , buyprice : 600});
fruitlist.push({fruit : "황도" , sellprice : 3000 , buyprice : 700});
for (var i = 0; i < fruitlist.length; i++) {
var f_sell = "<option value='3'>" + fruitlist[i].fruit + "</option>";
$('#select').append(f_sell);
};
$('#select').change(function() {
var indt = $("#select option:selected").text();
var indexf = fruitlist.findIndex(o => o.fruit == indt);
$('#price_k').text(fruitlist[indexf].sellprice);
});
</script>
질문 드립니다 ㅠ 이렇게 코드를 짜서 연습을 하고 있는데요 ㅠ
select 에서 fruit 사과를 선택하면 일단 sellprice 값이
<div id="price_k"></div> 여기에 출력하게 만들어놨습니다!
문제는 select 가 아무거 되어 있는 상황에서
radio 버튼을 sell 또는 buy로 변경시에
객체의 sellprice 에서 buyprice 값으로 각각 변경이 가능하게 하고싶은데요 ㅠ
코드를 짜는데서 막히네요 ㅠ 가장 좋은 방법이 어떻게 될까요?? ㅠ
!-->답변 2
select 변경 이벤트에
sell, buy 값을 받아 조건식으로 price_k 값을 넣어 주게 해야 할거 같구요.
input 변경 이벤트에
input 값과 select 값을 가져와 배열에서 맞는 가격을 찾아 price_k 에 넣어 주시면
될거 같습니다.
답변을 작성하시기 전에 로그인 해주세요.