使用jquery:选择了带有下一个/上一个箭头的选择器

时间:2012-05-05 09:50:34

标签: javascript jquery forms

我正在尝试为在线font预览目的设置小型网络应用程序。到现在为止一切顺利。我做了选择框(工作正常),测试文本区域(工作正常),font-sizer(工作正常),粗体,斜体的复选框...(工作正常),其中包含字体列表。当我从选择列表中选择一种字体时,页面上的所有内容(预定义文本)都会被更改,这样就可以了,但我决定添加箭头(下一页和上一页)以进行快速更改,这些更改也有效但有些元素未更改按钮单击。

实际问题是什么?

当我从选择列表中选择字体时,所有内容都已更改,但是当我将这些按钮用于next / prev H1时,页面标题不想更改,因为我需要在页面标题中显示字体名称以及页面标题TITLE

这里是:http://jsfiddle.net/S8PwY/

有人可以检查这个脚本并指出我的问题所在:

$(document).ready(function() {
$(".fontpicker").change(function() {
    $font = $('.fontpicker option:selected').val();
    $('.font_preview').css('fontFamily', $font);
});
$(".fontpicker").change(function() {
    $font = $('.fontpicker option:selected').val();
    $('.name').css('fontFamily', $font);
});



//min font size
var min = 10;

//max font size
var max = 60;

//grab the default font size
var reset = $('textarea').css('fontSize');

//font resize these elements
var elm = $('textarea');

//set the default font size and remove px from the value
var size = str_replace(reset, 'px', '');

//Increase font size
$('a.fontSizePlus').click(function() {

    //if the font size is lower or equal than the max value
    if (size <= max) {

        //increase the size
        size++;

        //set the font size
        elm.css({
            'fontSize': size
        });
    }

    //cancel a click event
    return false;

});

$('a.fontSizeMinus').click(function() {

    //if the font size is greater or equal than min value
    if (size >= min) {

        //decrease the size
        size--;

        //set the font size
        elm.css({
            'fontSize': size
        });
    }

    //cancel a click event
    return false;

});

//Reset the font size
$('a.fontReset').click(function() {

    //set the default font size    
    elm.css({
        'fontSize': reset
   }



$('.selector').click(function() {
    var checked = $(this).is(':checked');
    var value = $(this).attr('value');
    if (checked) {
        $('.font_preview, textarea').addClass(value);
    } else {
        $('.font_preview, textarea').removeClass(value);
    }
});

$("#next").click(function() {
    $("#mycars").val($("#mycars > option:selected").next().val());
    $font = $('.fontpicker option:selected').val();
    $('.font_preview').css('fontFamily', $font);
    $('.name').css('fontFamily', $font);

});

$("#prev").click(function() {
    $("#mycars").val($("#mycars > option:selected").prev().val());
    $font = $('.fontpicker option:selected').val();
    $('.font_preview').css('fontFamily', $font);
    $('.name').css('fontFamily', $font);
});

 $("select").change(function () {
      var str = "";
      $("select option:selected").each(function () {
            str += $(this).text() + " ";
          });
      $("title,h1.name,th.name").text(str);
    })
    .trigger('change'); 

2 个答案:

答案 0 :(得分:2)

您可以在每个change中触发click事件,以便更改内容:

$("#next").click(function() {
    $("#mycars").val($("#mycars > option:selected").next().val()).trigger("change");
});

$("#prev").click(function() {
    $("#mycars").val($("#mycars > option:selected").prev().val()).trigger("change");
});

DEMO: http://jsfiddle.net/u7dDw/

答案 1 :(得分:2)

可以按如下方式合并和修复代码:

$(".fontpicker").change(function() {
    $font = $('.fontpicker option:selected').val();
    $('.name').css('fontFamily', $font);
    $('.font_preview').css('fontFamily', $font);
    $("title,h1.name,th.name").text($font);
}).change();

$("#next").click(function() {
    $(".fontpicker").val($(".fontpicker >option:selected").next().val()).change();
});

$("#prev").click(function() {
    $(".fontpicker").val($(".fontpicker >option:selected").prev().val()).change();
});

请参阅updated fiddle