单击按钮更改选项

时间:2016-05-07 21:40:35

标签: javascript jquery

我正在尝试编写一个用户脚本,它会在下拉列表下添加按钮,并根据点击的按钮更改值。这里有3个单独的下拉列表:

'org.springframework.boot:spring-boot-starter-data-jpa:1.3.3.RELEASE'

<fieldset>
<div style="float: left;">
<div><label>Number of big seeds</label> <select class="form-control" name="big"> <optgroup label="big"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></optgroup> </select></div>

<div><label>Number of small seeds</label> <select class="form-control" name="small"><optgroup label="small"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></optgroup> </select></div>

这是我对用户脚本失败的尝试:

<div><label>Number of tiny seeds</label> <select class="form-control" name="tiny"><optgroup label="tiny"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option> </optgroup> </select></div>

有人能告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:0)

您立即绑定点击事件,但在文档准备好之前不添加按钮。在附加按钮后,将.click()调用放在$(document).ready()内。

$(document).ready(function() {

    //Variables for buttons
    var $0 = $('<button type="button" data-index="0" data-value="0">0</button>');
    var $1 = $('<button type="button" data-index="0" data-value="1">1</button>');
    var $2 = $('<button type="button" data-index="0" data-value="2">2</button>');
    var $3 = $('<button type="button" data-index="0" data-value="3">3</button>');
    var $4 = $('<button type="button" data-index="0" data-value="4">4</button>');
    var $5 = $('<button type="button" data-index="0" data-value="5">5</button>');

    var $6 = $('<button type="button" data-index="1" data-value="0">0</button>');
    var $7 = $('<button type="button" data-index="1" data-value="1">1</button>');
    var $8 = $('<button type="button" data-index="1" data-value="2">2</button>');
    var $9 = $('<button type="button" data-index="1" data-value="3">3</button>');
    var $10 = $('<button type="button" data-index="1" data-value="4">4</button>');
    var $11 = $('<button type="button" data-index="1" data-value="5">5</button>');

    var $12 = $('<button type="button" data-index="2" data-value="0">0</button>');
    var $13 = $('<button type="button" data-index="2" data-value="1">1</button>');
    var $14 = $('<button type="button" data-index="2" data-value="2">2</button>');
    var $15 = $('<button type="button" data-index="2" data-value="3">3</button>');
    var $16 = $('<button type="button" data-index="2" data-value="4">4</button>');
    var $17 = $('<button type="button" data-index="2" data-value="5">5</button>');


    //Add buttons
    $('.form-control').eq(0).after($0,$1,$2,$3,$4,$5);
    $('.form-control').eq(1).after($6,$7,$8,$9,$10,$11);
    $('.form-control').eq(2).after($12,$13,$14,$15,$16,$17);

    //Click event
    $(":button").click(function(){
         var thisIndex = $(this).data('index');
         var thisValue = $(this).data("value");

         $('.form-control').eq(thisIndex).val(thisValue);
    });
});

您也不应该为元素提供重复的ID。我已将其更改为使用数据属性。虽然复制类是可以的,但仅使用它们来保存有关元素的数据并不合适,所以我也将它们更改为数据属性。