【问题标题】:I have a textbox and button. I want to show alert with JQuery我有一个文本框和按钮。我想用 JQuery 显示警报
【发布时间】:2015-01-15 09:27:39
【问题描述】:

我有一个文本框和按钮。我想在单击按钮时显示alert。但我有一个条件;输入文本值必须是X1。如果用户在文本框中写入X1,则显示警报GOOD,否则显示警报NOT GOOD

如何使用 jQuery 做到这一点?这是我的html代码。

<input type="text" id="couponInput" class="couponInput" placeholder="Input Code" />                                                       
<button type="button" id="couponApply" class="couponApply">APPLY</button>

【问题讨论】:

  • 这读起来像个简报。你有没有尝试过自己解决这个问题?
  • 阅读文档:这里有一些你应该需要的方法valclick。看看选择器:api.jquery.com/category/selectors
  • @RoryMcCrossan 你是对的。我是 JQuery 的新手,当然我做了一些尝试,但它们不起作用。
  • 这太基本了,我不敢相信您在 WEB 上找不到任何东西来解决您的预期行为。做一些搜索...

标签: javascript jquery html input


【解决方案1】:

如果您想要特定的 jquery 答案。在这里....

$(document).ready(function(){ // Document ready event -- most important

    $('#couponApply').click(function(event){

        var couponInputval = $('#couponInput').val(); // Getting the input value
        couponInputval = couponInputval.toUpperCase(); // Changing to upper case
        if(couponInputval == 'X1') {
            alert("GOOD");
        }
        else{
            alert("NOT GOOD");
        }
        event.preventDefault(); // To restrict button to push request to the server

    });


});

希望对你有帮助......

【讨论】:

    【解决方案2】:

    添加这个 javascript

    //This detect the onClick event on your button
    $("#couponApply").click(function(){
    //here you retrieve the value of your input
    var inputValue = $("#couponInput").val();
    //And now, you test it
    if(inputValue == "X1")
        alert("GOOD");
    else
        alert("NotGOOD");
    });
    

    你可以测试一下here

    【讨论】:

      【解决方案3】:

      你不需要 jQuery,但试试这个

      <button type="button" id="couponApply" class="couponApply" onclick="if($('#couponInput').val() == 'X1'){alert('GOOD');} else {alert('NOT GOOD');}">APPLY</button>
      

      此外,如果您尝试验证优惠券,那么这不是一个好方法 - 您的代码在您网站的源代码中很容易阅读 - 您应该在服务器端执行此操作

      【讨论】:

      • 这是使用内联脚本的坏建议,无论如何你在这里使用 jQuery...
      • @berry 我的答案不是最好的,但它很容易实现。以后尝试使用 $('#couponApply').click()。
      猜你喜欢
      • 2013-05-25
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 2014-05-18
      • 2015-07-15
      相关资源
      最近更新 更多