How to display the selected items of jquery checkbox list along with the label when only the user selects that particular checkbox

后端 未结 1 868
Happy的楠姐
Happy的楠姐 2021-01-16 21:36
 
1条回答
  •  失恋的感觉
    2021-01-16 22:26

    FIDDLE

    $("select[name='qwe'] option").click(function () {
        $(this).each(function () {
            var label = $(this).parent().parent().parent().find('label').text();
            var text = $(this).text();
            alert('option text is ' + text);
            alert('label text is ' + label);
        });
    })
    

    UPDATE

    As @mplungjan suggested FIDDLE

    $("select[name='qwe']").change(function () {
        $(this).each(function () {
            var text1 = $(this).find('option:selected').text();
            var label1 = $(this).find('option:selected').parent().parent().parent().find('label').text();
            alert('option text is ' + text1);
            alert('label text is ' + label1);
        });
    

    UPDATE

    No .parent() with .closest('div.classnameoftheparentdiv')

    FIDDLE

    $("select[name='qwe']").change(function () {
        $(this).each(function () {
            var text1 = $(this).find('option:selected').text();
            var label1 = $(this).find('option:selected').closest('div.form_field').find('label').text();//HERE parent div that contains the label has a class form_field
            alert('option text is ' + text1);
            alert('label text is ' + label1);
        });
    })
    

    0 讨论(0)
提交回复
热议问题