채택완료

글 수정 시, show hide 초기화가 되는데, 그대로 값 불러오게끔 하려 합니다.

Copy
<script>
                        $(function(){$("#wr_3").val("<?=$write['wr_3[]']?>");});
                        $("tr.aa").hide();
                        $('input[name=wr_1], input[name=wr_2], input[name=wr_4], input[name=wr_7], select[name=wr_3]').on('change', function() {
                            var wr_1 = $('input[name=wr_1]:checked').val();
                            var wr_2 = $('input[name=wr_2]:checked').val();
                            var wr_4 = $('input[name=wr_4]:checked').val();
                            var wr_7 = $('input[name=wr_7]:checked').val();

                            if (wr_1 == "a1" && wr_2 == "s1" && wr_4 == "d1" && wr_7 == "f1") {
                                $('tr.sel1111').show();
                                $('tr.sel1111').siblings("tr.aa").hide();
                            }
                            else if (wr_1 == "a1" && wr_2 == "s1" && wr_4 == "d1" && wr_7 == "f2") {
                                $('tr.sel1112').show();
                                $('tr.sel1112').siblings("tr.aa").hide();
                            }
                            else {
                                $('tr.aa').hide();
                            }
                            
                        });
                    </script>

write.skin.php에 이 스크립트를 써서 사용하고 있는 상태인데, 글 수정 시 4개의 라디오버튼 선택이 충족 되어있는데, show가 되어있지 않습니다. ㅠㅠ 

근데, 라디오버튼 선택을 다른것으로 선택하고 다시 충족하게되면 show가 되어지는데, 글 수정 시, get으로 값을 가져와서 충족이 되어있는 상태이면 show를 하고싶은데, 코드상에 문제가 있을까요?

|

답변 1개 / 댓글 19개

채택된 답변
+20 포인트
2021-04-26 (월) 16:57:18

.on('change', functionI() ~

은 라디오 버튼 또는 체크박스의 값이 (유저 액션에 의해) 변경되는 이벤트가 발생했을 때만 실행됩니다.

수정화면에서는.. 유저액션 없이 페이지가 로드될 때 수행해주어야 되기 때문에..

해당 부분을 수동으로 호출해주면 됩니다.

Copy
<script>
  $(function(){$("#wr_3").val("<?=$write['wr_3[]']?>");});
  $("tr.aa").hide();
  $('input[name=wr_1], input[name=wr_2], input[name=wr_4], input[name=wr_7], select[name=wr_3]').on('change', function() {
     change_action(); // 별도 함수화 처리
  });
  
  function change_action() {
      var wr_1 = $('input[name=wr_1]:checked').val();
    var wr_2 = $('input[name=wr_2]:checked').val();
    var wr_4 = $('input[name=wr_4]:checked').val();
    var wr_7 = $('input[name=wr_7]:checked').val();
    if (wr_1 == "a1" && wr_2 == "s1" && wr_4 == "d1" && wr_7 == "f1") {
        $('tr.sel1111').show();
        $('tr.sel1111').siblings("tr.aa").hide();
    }
    else if (wr_1 == "a1" && wr_2 == "s1" && wr_4 == "d1" && wr_7 == "f2") {
        $('tr.sel1112').show();
        $('tr.sel1112').siblings("tr.aa").hide();
    }
    else {
        $('tr.aa').hide();
    }
  }
  
  <?php if ($w == 'u') { ?>
      change_action(); // 수정시에는 수동으로 직접 호출
  <?php } ?>
</script>

답변에 대한 댓글 19개

아되 긴하는데, 선택된 셀렉트값이 나오지않고, "선택하세요"옵션 처음값으로만 나와지네요 ㅠㅠ

$(function(){$("#wr_3").val("<?=$write['wr_3[]']?>");});

이부분이 안먹히는거같아서 스크립트문에 삽입했는데도 먹히질 않네요 ㅠㅠ
2021-04-26 (월) 17:47:10
$(function() {
$("#wr_3").val("<?php echo $write['wr_3'] ?>");
});
[code]
<script>
$(document).ready(function(){
$("#bo_list .srch_tbl tr label input[type='radio']").change(function(){
if($("#bo_list .srch_tbl tr label input[type='radio']").is(":checked")){
$(this).parent().addClass('checked');

} else {
$(this).parent().siblings().removeClass('checked');
}
});

});
</script>
[/code]

혹시 이부분도 봐주실 수 있나요? 라디오버튼에 체크가 되어있을 시, 스타일클래스를 넣으려고 하는데 적용이 되지 않아서요 ㅠㅠ
2021-04-26 (월) 18:40:07
[code]
<script>
$(function() {
$("#bo_list .srch_tbl input[type='radio']").on('change', function() {
if ($(this).is(":checked")){
$(this).parent().addClass('checked');
} else {
$(this).parent().siblings().removeClass('checked');
}
});
});
</script>
[/code]
순간적으로 변하긴하는거같은데,

[code]
<script>
$('input[name=wr_1], input[name=wr_2], select[name=wr_3], input[name=wr_4], input[name=wr_7], select[name=sca], input[name=sca]').on('change', function() {
this.form.submit();
});
</script>
[/code]

이부분을 써서 그런지 서브밋되면 다시 풀려버리네요 ㅠㅠ
2021-04-26 (월) 18:52:46
on('change', ~
는 유저 액션으로 라디오 값이 변경될 때만 실행합니다.

서브밋되면 페이지가 새로 로딩되기 때문에..
유저 액션이 없는 경우에는 실행되지 않습니다.
아 그러면 체크가 되어있는 상태일때 배경색을 변경하려면 어떻게 해야하죠 ㅠㅠ?
[code]
#bo_list .srch_tbl label input[type='radio']:checked ~ label {
background: #5e7aa0;
font-weight: 500;
color: #fff;
}
[/code]

css로 구현하려하는데, 인풋이 라벨 안에 있다보니까.... 연산자를 어떻게 사용해야할지를 모르겠네요 ㅠㅠ
2021-04-26 (월) 19:01:42
<script>
$(function() {
$("#bo_list .srch_tbl input[type='radio']").on('change', function() {
if ($(this).is(":checked")){
$(this).parent().addClass('checked');
} else {
$(this).parent().siblings().removeClass('checked');
}
});
});

<?php if ($w == 'u') { ?>
$("#bo_list .srch_tbl input[type='radio'][value='<?php echo $write['wr_100'] ?>']").prop('checked', true);
<?php } ?>
</script>
정말 고생해서 답변주셨는데 ㅠㅠ 적용이 되지 않네요 ㅠㅠ
2021-04-26 (월) 19:22:33
<?php if ($w == 'u') { ?>
$(function() {
$("#bo_list .srch_tbl input[type='radio'][value='<?php echo $write['wr_100'] ?>']").prop('checked', true).trigger('change');
});
<?php } ?>


$(function() {
$("#bo_list .srch_tbl input[type='radio'][value='1']").prop('checked', true);
});

$(function() {
$("#bo_list .srch_tbl input[type='radio'][value='1']").prop('checked', true).trigger('change');
});
페이지가 로딩될 때 value 가 1인 항목으로 체크하라는 스크립트인데.. 이런 코드로 테스트를 해볼 수 있습니다. https://stackoverflow.com/a/4618748

https://m.blog.naver.com/PostView.nhn?blogId=doryjj&logNo=199027914
너무 감사드려요 ㅠㅠ 암만봐도 해결은 되지 않았지만 ㅠㅠ 정말 감사드립니다 ㅎ

[code] #bo_list .srch_tbl label input[type='radio']:checked [/code]

이거를 어찌어찌 응용해서 하면 될 것 같긴한데,,,,, 인풋이 라벨 안에 있으니 참 답답하네요 ㅠㅠ
2021-04-26 (월) 19:35:46
<label><input type="radio" name="wr_1" value="1" <?php echo $write['wr_1'] == "1" ? 'checked'
: '' ?>></label>
<label><input type="radio" name="wr_1" value="2" <?php echo $write['wr_1'] == "2" ? 'checked' : '' ?>></label>
<label><input type="radio" name="wr_1" value="3" <?php echo $write['wr_1'] == "3" ? 'checked' : '' ?>></label>

http://www.gnuwiz.com/bbs/board.php?bo_table=board_tip&wr_id=16
진짜공부 많이해야할거같아요 ㅠㅠ 해결방안이 도무지 안나오네요 ㅠㅠ ㅎㅎ 도움주셔서 감사합니다 ㅎ
2021-04-26 (월) 19:45:27
[code]
#bo_list .srch_tbl input[type='radio']:checked ~ * {
background: #5e7aa0;
font-weight: 500;
color: #fff;
}
[/code]

https://stackoverflow.com/a/20547506

https://jsfiddle.net/dmpbn123/
[code]
#bo_list .srch_tbl tr label input[type='radio']:checked ~ * {
background: #5e7aa0;
font-weight: 500;
color: #fff;
[/code]

이렇게 사용을 해보았는데 안변하길래, 리플래시때문인가 해서 리플래시 되는 부분을 삭제하고했는데도 먹통이네요 ㅠㅠ
어... 스펜으로 감싸니까 뭔가 되는느낌인데, ...ㅋㅋ 라벨 전체 색상이 바껴야하는데 스펜 영역만 변경이 되버리니 ㅠㅠ
2021-04-26 (월) 20:26:28
https://jsfiddle.net/fLd1b0m2/

or


<label><input type="radio" name="wr_1" value="1" <?php echo $write['wr_1'] == "1" ? 'checked'
: '' ?>></label>
<label><input type="radio" name="wr_1" value="2" <?php echo $write['wr_1'] == "2" ? 'checked' : '' ?>></label>
<label><input type="radio" name="wr_1" value="3" <?php echo $write['wr_1'] == "3" ? 'checked' : '' ?>></label>
<script>
$('input[name=wr_1]:checked').parent().addClass('checked');
</script>
<style>
#bo_list .srch_tbl label.checked {
background: #5e7aa0;
font-weight: 500;
color: #fff;
}

</style>
아저도 어제 새벽에 여러가지 방법으로 해서 해봤는데, 말씀주신 내용처럼 하니 진행되었어요 ㅎㅎ 감사합니다 ㅎ

답변을 작성하려면 로그인이 필요합니다.