【问题标题】:If not enough money disabled button?如果没有足够的钱禁用按钮?
【发布时间】:2016-08-10 21:31:04
【问题描述】:

我有一个问题。

这是我的按钮:

$balance = "3";
    <button type="submit" type=tex name="log" class="btn btn-primary">Ticket kaufen! Je mehr, desto mehr Chance habt ihr zu gewinnen!</button>

所以,我的问题是:

如果用户没有足够的钱,我希望按钮被禁用!

有可能吗?使用 Javascript 还是 JQuery?

对不起我的英语! 我希望你能帮助我!

谢谢

编辑:

我已经厌倦了这个! :

    $balance = "3";
        <button type="submit" type=tex name="log" class="btn btn-primary">Ticket kaufen! Je mehr, desto mehr Chance habt ihr zu gewinnen!</button>

<script>
$('button').attr('1838383', $balance)

if ($('button').attr('193939') < COSTOFITEM) {
 $('button').hide()
}
</script>

但这不起作用

【问题讨论】:

标签: javascript jquery button


【解决方案1】:

我假设您正在使用引导程序。

我假设您有一个输入字段,用户可以在其中输入一个数值,当它小于 3 美元时,必须禁用该按钮。

您需要为输入字段添加事件处理程序,以便每次用户更改值时都必须进行测试以启用或禁用按钮。

我尝试用 sn-p 解释如何达到这个结果:

// when the document is loaded
$(function () {
  
  // every time the user change the input value
  $('#balance').on('input', function (e) {

    // set the property disabled of the button:
    //
    // 'button[name="log"]': means select a button with the
    // attribute name equal to the string log
    //
    //(+this.value < 3): means: translate the input value to
    // a number and test if it is less then 3
    //
    $('button[name="log"]').prop('disabled', (+this.value < 3));
  })
});
<!--
include the jquery and bootstrap 
-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>


<!-- I assume you have a form like...  -->
<form action="#">
    <div class="form-actions">
        <div class="input-group">
            <span class="input-group-addon">$</span>
            <input type="number" value="1000" class="form-control currency" id="balance"/>
        </div>
        <button type="submit" name="log" class="btn btn-primary">Ticket kaufen! Je mehr, desto mehr Chance habt ihr zu
            gewinnen!
        </button>
    </div>
</form>

【讨论】:

  • @TheBossInTheHouse 在您的 PHP 文件中,您生成一个 HTML 文档。因此,您需要将“包含 jquery 和 bootstrap”库添加到生成的 HTML 的 head 部分。相反,您需要在正文中插入“我假设您有一个类似...的表格”。最后,你需要添加一个包含我的javascript的脚本标签。
  • Oke in Html 工作这个!我试着把它放在我的 php 文件中!谢谢!
  • @TheBossInTheHouse 对不起,我在客户端帮助了你,现在你需要做一些努力来编写服务器端。这不是那么难。尝试用谷歌搜索互联网。如果您找不到解决方案,请尝试提出另一个问题,但这次问题必须包含可信的内容。
【解决方案2】:

您可以使用 jquery 的prop 函数启用和禁用按钮。

您可以为您的按钮创建一个id。并在$balance 不超过一定数量时禁用它。

按照下面的代码,这可能会得到你想要的。

HTML:

<button id="submitButton" type="submit" type=tex name="log" class="btn btn-primary">Ticket kaufen! Je mehr, desto mehr Chance habt ihr zu gewinnen!</button>

JS:

if($balance < COST)
{
    $("#submitButton").prop("disabled",true);
}
else
{
    $("#submitButton").prop("disabled",false);
}

希望这会有所帮助。

【讨论】:

  • 您不能在HTML 代码中使用$balance。那是行不通的。它应该存在于javascript
  • 我已经检查过了,它正在工作。在你的javascript代码中添加$balance = 3,然后再次检查。你会看到它的工作。
猜你喜欢
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多