Jquery操作radio的簡單一實例_jquery

來源:互聯網
上載者:User

<input name="study" type="radio" value="Jquery外掛程式教程">Jquery外掛程式教程
<input name="study" type="radio" value="Jquery學習">Jquery學習
<input name="study" type="radio" value="PHP學習">PHP學習
<input id="tijiao" type="button" value="確定">
<input id="set_jqeury_study" title="Jquery學習" type="button" value="將radio設定為Jquery學習">
<input name="study" type="text" value="請輸入您想學到的知識">
<input id="view_input_text" type="button" value="點擊我,查看到的是同樣名為‘study',但是類型是TEXT的輸入框裡面的值">

在上面的html代碼中,細心的您應該可以發現3個radio的name屬性和一個text的name屬性都是“study”

複製代碼 代碼如下:

$(function(){
$('#tijiao').click(function(){
if ($("input:[name=study]:radio:checked").length == 0){
alert("請選擇您想學習的知識");
return false;
}
var study = $("input:[name=study]:radio:checked").val();
alert("謝謝!您選擇的是" + study);
})
$("#set_jqeury_study").click(function(){
var $button = $(this);
$("input:[name=study]:radio").each(function(){
if (this.value == $button.attr('title')){
this.checked=true;
}
})
})
$("#view_input_text").click(function(){
alert($("input[name=study]:text").val());
})
})

$(“input:[name=study]:radio:checked”)這段代碼取得的是所有name屬性為“study”而且已經被選中的radio的jquery對象,通過判斷他的length 是否等於0,就可以知道這個radio選項是否有一個被選中了。$(“input[name=study]:text”)這段代碼取得的是name屬性為“study”的text輸入框的jquery對象。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.