在jquery mobile的環境下,想要在點選未聯絡上的checkbox選項時,是否繼續的選項(用select data-role='slider' 控制)會自動轉成繼續,而不是預設的放棄,
請問該怎麼透過jquery控制,才能達到這個效果?
預設時是這樣,請參考下圖:
想要一點選未聯絡上,就會轉成繼續的選項,請參考下圖:
但是怎麼試,都還是預設的選項:
我也試了selectmenu('refresh')的方法,但是效果更糟,竟然整個"未聯絡上"都無法選取了...`
		$('#not_contacted').change(function(){ 			
		if($('#not_contacted').is(':checked')){
			$('#slider').val('on').selectmenu('refresh');
		}else{
			$('#slider').val('off').selectmenu('refresh');
		}
			
		});
		});
	</script>
</head>
<body>
	<div data-role='page'>	
		<div data-role='header'>
			
			<h1>繼續聯絡</h1>
				
		</div>
		<div data-role='content'>
			<form data-ajax='false' method='post' action=''>
			<div data-role='fieldcontain'>
				<label for='not_contacted'>未聯絡上</label>
				<input type='checkbox' id='not_contacted' name='not_contacted' value='2'>
			</div>
			
			<div data-role="fieldcontain">
			<label for="slider">是否繼續:</label>
				<select name="slider" id="slider" data-role="slider">
					<option value="on">繼續</option>
					<option value="off" selected>放棄</option>
					
				</select> 
			</div>
			
		</div>
	</div>
</body>
[是否繼續]這個選項,用flipswitch widget來做比較正規。
要改變flipswitch的選取狀態,先要用JQuery的val方法去改變select的選取值,然後用flipswitch的refresh方法去更新圖像。
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
</head>
    
<body>
    <div data-role="content">
        <div data-role="fieldcontain">
            <label for="not-contacted">未聯絡上</label>
            <input type="checkbox" id="not-contacted" name="not-contacted" value="2">
        </div>                    
        <div data-role="fieldcontain">
            <label for="continue">是否繼續:</label>
            <select name="continue" id="continue" data-role="flipswitch">
                <option value="on">繼續</option>
                <option value="off" selected>放棄</option>
            </select> 
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>     
    
    <script>
        $(document).ready( function() {
            $('#not-contacted').change( function() {
                if (this.checked) {
                    $('#continue').val('on').flipswitch('refresh');
                }
            });
        });
    </script>
</body>    
</html>	
<body>
		<div data-role='page'>	
		<div data-role='header'>
			
			<h1>繼續聯絡</h1>
				
		</div>
		<div data-role='content'>
			<form data-ajax='false' method='post' action=''>
			<div data-role='fieldcontain'>
				<label for='not_contacted'>未聯絡上</label>
				<input type='checkbox'  id='not_contacted' name='not_contacted'>
				
			</div>
			
			<div data-role="fieldcontain">
			<label for="slider">是否繼續:</label>
				<select name="slider" id="slider" data-role="slider">
					<option id="slider_on" value="on" >繼續</option>
					<option id="slider_off" value="off" selected>放棄</option>
					
				</select> 
			</div>
			</form>
		</div>
	</div>
</body>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>	
  <script>
$(document).ready(function(){
 	$('#not_contacted').click(function(){ 	
	$('#slider option').prop('selected',false);	
	if($('#not_contacted').prop('checked')==true){	
		$('#slider_on').prop('selected',true);
	}
	else{
		$('#slider_off').prop('selected',true);
	}		
	});
});
  </script>	
試試看
你判斷式一定要確定回傳值是對的,不然你怎麼判斷當然都沒反應
使用前先了解jquery的 checkbox 和 select 怎麼取值吧
https://goo.gl/qcYY38 checkbox
https://goo.gl/BsXn7Y select
主要透過觀察下方的繼續和放棄的Elements變化後,再透過繼續聯絡的點擊事件直接手動去修改Elements變化!而修改變化的同時也給他正確的值(on或off)。
如果有問題再麻煩你留言告訴我!謝謝