2017-06-23 62 views
1

我想爲$(「。選擇器」)使用this.value,$(「#style_background」)是我的保存按鈕。當我使用這個代碼就像this.value即將'保存',我怎麼能做到這個dania,seablue。使用此作爲第二個變量

$("#style_background").click(function(){ 

    if ($(".selector").is(":checked")) { 

     document.cookie = "background=" + this.value + ";" + "path=/"; 

    } else {} 

}); 

HTML

<li> 
    <input type="radio" class="selector" id="cookie_dania" name="background" value="dania"> 
</li> 
<li> 
    <input type="radio" class="selector" id="cookie_seablue" name="background" value="seablue"> 
</li> 
+2

*我想用THIS.VALUE爲$( 「選擇 」)的雙選中的值使用$(「 #style_background」)是我保存按鈕*不是很清楚 – guradio

+0

「background =」+ this.value即將到來保存我想要獲得$(「。selector」)。使用此值 – d3esligner

回答

0

嘗試選擇器:checkedeach用於逐個選擇選中的單選按鈕的值。僅選擇了射頻按鈕。您需要的checkbox代替radio按鈕

$("#style_background").click(function() { 
 
    $(".selector:checked").each(function(a){ 
 
// if ($(".selector").is(":checked")) { 
 
    //document.cookie = "background=" + $(this).val() + ";" + "path=/"; 
 
// } else {} 
 
    console.log($(this).val()) 
 
    }) 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<li> 
 
    <input type="checkbox" class="selector" id="cookie_dania" name="background" value="dania"> 
 
</li> 
 
<li> 
 
    <input type="checkbox" class="selector" id="cookie_seablue" name="background" value="seablue"> 
 
</li> 
 
<button id="style_background">save</button>

1

在你的代碼this是指style_background選擇,你必須綁定點擊事件,所以你需要通過特定的選擇爲

$("#style_background").click(function(){ 

    if ($(".selector").is(":checked")) { 

     document.cookie = "background=" + $(".selector:checked").val() + ";" + "path=/"; 

    } else {} 

}); 
+0

您只需要獲取'checked'選擇器的值 –

相關問題