如果没有足够的钱禁用按钮

If not enough money disabled button?

本文关键字:钱禁用 按钮 如果没有      更新时间:2023-09-26

我有一个问题。

这是我的按钮:

$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>

但是这行不通

我假设您使用的是bootstrap。

我假设你有一个输入字段,用户可以输入一个数字值,当它少于3美元时,按钮必须禁用。

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

我试着用一个代码片段来解释如何实现这个结果:

// 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>

您可以使用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);
}