jQuery+PHP擷取Select option 取捨的Text和Value(附選擇城市執行個體)

來源:互聯網
上載者:User
jQuery+PHP擷取Select option 選擇的Text和Value(附選擇城市執行個體)


PHP代碼:

                 
  • 所在城市 選擇所在城市 <%foreach from=$supportcity item=city key=k%> selected<%/if%> ><%$city.city_name%> <%/foreach%>
  • 所在地區 <%foreach from=$supportcity item=city key=k%> 選擇所在地區 <%foreach from=$city.areas item=area%> <%$area.area%> <%/foreach%> <%/foreach%>

  • Jquery代碼:

            $("select.city").change(function(){            var ccode = $(this).val();            $("select.area").hide();            if(ccode){                $("select.area[citycode="+ccode+"]").show();            }        });        var sltcity = $("select.city").val();        if(sltcity){             $("select.area[citycode="+sltcity+"]").show();        }

    下面是詳細的:

    jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關設定

    擷取一組radio被選中項的值:var item = $('input[name=items][checked]').val();
    擷取select被選中項的文本:var item = $("select[@name=items] option[@selected]").text();

    擷取select被選中項的文本 :var item = $("select[name=items] option[selected]").text(); 或$("select[name=items]").find("option:selected").text();

    select下拉框的第二個元素為當前選中值:$('#select_id')[0].selectedIndex = 1;

    select下拉框value = 'val'的元素為當前選中項:$("select[name=items] option[value='val']").attr("selected","selected");

    radio單選組的第二個元素為當前選中項 :$('input[@name=items]').get(1).checked = true; 或$('input[name=items]').attr("checked", '1′);

    radio的value = 'val'的元素為當前選中項:$('input[name=items] [value='val']').attr("checked","checked");


    擷取值:

    文字框,文本地區:$("#txt").attr("value");

    多選框checkbox:$("input[name='checkbox':checked]").each(function(){

    var val = $(this).val();

    });

    單選組radio: $("input[type=radio][checked]").val();

    下拉框select的value值: $('select').val();

    下拉框select選中的text 值:$("select").find("option:selected").text();

    控製表單元素:

    文字框,文本地區:$("#txt").attr("value","); //清空內容

    $("#txt").attr("value",'11′); //填充內容

    多選框checkbox:
    checkbox的第二個元素被打勾:$("input[name=items]").get(1).checked = true; //打勾
    $("input[name=items]").get(1).checked = false; //不打勾

    checkbox的value='val'的元素前打勾:$("input[name=item][value='val']").attr("checked",true); 或$("input[name=item][value='val']").attr("checked","checked");

    if($("input[name=item][value='val']").attr('checked')==true) //判斷是否已經打勾

    單選組radio: $("input[type=radio]").attr("checked",'2′);//設定value=2的項目為當前選中項

    下拉框select: $("#sel").attr("value",'-sel3′);//設定value=-sel3的項目為當前選中項

    $("2222").appendTo("#sel")//添加下拉框的option

    $("#sel").empty();//清空下拉框

    jQuery擷取Radio選擇的Value值


    代碼

    $("input[name='radio_name'][checked]").val(); //選擇被選中Radio的Value值
    $("#text_id").focus(function(){//code...}); //事件 當對象text_id擷取焦點時觸發
    $("#text_id").blur(function(){//code...}); //事件 當對象text_id失去焦點時觸發
    $("#text_id").select(); //使文字框的Vlaue值成選中狀態
    $("input[name='radio_name'][value='要選中Radio的Value值'").
    attr("checked",true); //根據Value值設定Radio為選中狀態

    jQuery擷取CheckBox選擇的Value值

    $("input[name='checkbox_name'][checked]"); //選擇被選中CheckBox元素的集合 如果你想得到Value值你需要遍曆這個集合
    $($("input[name='checkbox_name'][checked]")).
    each(function(){arrChk+=this.value + ',';});//遍曆被選中CheckBox元素的集合 得到Value值
    $("#checkbox_id").attr("checked"); //擷取一個CheckBox的狀態(有沒有被選中,返回true/false)
    $("#checkbox_id").attr("checked",true); //設定一個CheckBox的狀態為選中(checked=true)
    $("#checkbox_id").attr("checked",false); //設定一個CheckBox的狀態為不選中(checked=false)
    $("input[name='checkbox_name']").attr
    ("checked",$("#checkbox_id").attr("checked"));//根據3,4,5條,你可以分析分析這句代碼的意思
    $("#text_id").val().split(","); //將Text的Value值以','分隔 返回一個數組

    jquery1.3.2

    ISDARK : $("input[@type=radio][name=ISDARK][checked]").val()

    $("input[name=radioname][value=radio值]").attr("checked","checked");



  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.