简体   繁体   English

单击时更改按钮的值

[英]Change value of a button when clicked

I have a list of jokes that I want to display in a box. 我有一个要在框中显示的笑话列表。 When the page is loaded I want to display one at random in the box. 加载页面后,我想在框中随机显示一个。 Then when I click the box I want it to change the value again to another random one from the list. 然后,当我单击该框时,我希望它再次将值更改为列表中的另一个随机值。

At the moment I just cannot get it to work and can't work out where I am going wrong. 目前,我只是无法正常工作,也无法解决我要去的地方。 I have the HTML as follows: 我有如下的HTML:

<input class="box" type="button" value"" />
<ul>
    <li>How much do pirates pay to get their ear pierced? &lt;/br&gt; A buck anear!</li>
    <li>How do pirates talk to one another? &lt;/br&gt; Aye to aye!</li>
    <li>What did the sea say to the pirate captain? &lt;/br&gt; Nothing, it justwaved!</li>
    <li>What is a pirates favourite shop? &lt;/br&gt; Ar-gos!</li>
    <li>When is it the best time for a pirate to buy a ship? &lt;/br&gt; Whenit is on sail!</li>
</ul>

The script is: 脚本是:

    $(document).ready(function () {

    var list = $("ul li").toArray();
    var elemlength = list.length;
    var randomnum = Math.floor(Math.random() * elemlength);
    var randomitem = list[randomnum];

    $('.box').click(function () {
        $(this).val(randomitem);
    });
});

Can someone please show me how to make the button have a value when the page is loaded and then change the value when the button is clicked. 有人可以告诉我如何在加载页面时使按钮具有值,然后在单击按钮时更改该值。

Fiddle: http://jsfiddle.net/uRd6N/98/ 小提琴: http : //jsfiddle.net/uRd6N/98/

Well you have two problems. 好吧,你有两个问题。

The first is that you are always selecting the same joke (determined when the page is loaded). 第一个是您总是选择相同的笑话(由页面加载时决定)。 The second is that you are adding the whole li element to the value of the button. 第二个是将整个li元素添加到按钮的值。

Try this: 尝试这个:

$(document).ready(function () {
    var list = $("ul li").toArray();
    var elemlength = list.length;

    $('.box').click(function () {
        // get a new joke every time a button is clicked
        var randomnum = Math.floor(Math.random() * elemlength);
        var randomitem = list[randomnum].innerHTML; // use the HTML of the <li>
        $(this).val(randomitem);
    });
});

Live example: http://jsfiddle.net/NxdEc/ 实时示例: http//jsfiddle.net/NxdEc/

You don't need to load a jQuery object just to set a value. 您无需仅为了设置值就加载jQuery对象。 Also your randomitem needed to explicitely get the innerText of the li element 另外,您的randomitem需要显式获取li元素的innerText

$(document).ready(function () {

    var list = $("ul li").toArray();
    var elemlength = list.length;
    var randomnum = Math.floor(Math.random() * elemlength);
    var randomitem = list[randomnum].innerText;

    $('.box').click(function () {
        this.value = randomitem;
    });
});

Substitute 替代

$(this).val(randomitem);

with: 与:

$(this).val($(randomitem).text());

PS: or even better PS:甚至更好

$(document).ready(function () {

    var list = $("ul li");
    var elemlength = list.length;
    var randomnum = Math.floor(Math.random() * elemlength);
    var randomitem = list.eq(randomnum);

    $('.box').click(function () {
        this.value = randomitem.text();
    });
});

PPS: if you wanted to make the button a div you could just do similarly: PPS:如果您想将按钮设为div,则可以执行类似的操作:

$(document).ready(function () {

    var list = $("ul li");
    var elemlength = list.length;
    var randomnum = Math.floor(Math.random() * elemlength);
    var randomitem = list.eq(randomnum);

    $('.box').click(function () {
        $(this).html(randomitem.text());
    });
})

Just be sure to change the styiling of the div because it wouldn't look the same as that of an input. 只要确保更改div的样式即可,因为它的外观与输入的外观不同。

You should make a few changes in your code: 您应该对代码进行一些更改:

  • Generate random value everytime you click the button. 每次单击按钮都会生成随机值。 In your code random value is generated only once. 在您的代码中,随机值仅生成一次。
  • Assign element text, not the element itself, when clicking the button. 单击按钮时,分配元素文本,而不是元素本身。
  • Set an initial value. 设置初始值。

Random generation and assignment encapsualtion within a function would do great in your code. 函数中的随机生成和赋值封装将在您的代码中发挥更大的作用。 This code is working now (can run it at http://jsfiddle.net/uRd6N/99/ ): 该代码现在可以运行(可以在http://jsfiddle.net/uRd6N/99/上运行):

$(document).ready(function () {
    var list = $("ul li").toArray();
    var elemlength = list.length;

    // Encapsulate random value assignment within a function
    var setRandomValue = function(element) {
            var randomnum = Math.floor(Math.random() * elemlength);
            var randomitem = list[randomnum];
            $(element).val($(randomitem).text());
    }

    // Bind click button
    $('.box').click(function () {
        setRandomValue(this);
    });

    // Set random value for the first time
    setRandomValue($('.box'));
});

Try this fiddle (It's a fork of yours): http://jsfiddle.net/darshanags/qHpkk/1/ 试试这个小提琴(这是您的叉子): http : //jsfiddle.net/darshanags/qHpkk/1/

HTML (update): HTML(更新):

<input class="box" type="button" value="Value at page load" />

jQuery: jQuery的:

$(document).ready(function () {

  $('.box').click(function () {

    var $items = $("ul li"),
        list = $items.toArray(),
        elemlength = list.length,
        randomnum = Math.floor(Math.random() * elemlength);

     $(this).val($items.eq(randomnum).text());
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM