【问题标题】:If condition doesn't work in javascript/jquery?如果条件在 javascript/jquery 中不起作用?
【发布时间】:2014-02-26 10:52:18
【问题描述】:

我有一个在DataList 中显示在asp.net 中的产品列表。我在DataList 中有一个TextBox,其中包含包含Price 的产品。这个Textbox用于修改产品的Price。我只想在用户输入的PriceLESS THAN 时向用户显示消息,Minimum 价格设置为使用JQuery/Javascript 的产品。

一切正常。就在我使用Class 名称验证文本框时,它仅适用于第一个TextBox,仅适用于DataList,不适用于剩余的TextBox(当我在TextBox 中输入金额10 时(考虑到最低价格为50)必须转到脚本的“if”部分。但是,它转到脚本中的“else”部分)在 DataList 中。即 LESS THAN 符号仅适用于 DataList 中的 FirstItem 而不适用于其他 TextBox

在内容标签中:

<script language="javascript" type="text/javascript">
       // $(".numericText").change(function() {alert("changecalled") });

        $(document).on("change", ".numericText", function() {
            var id = $(this).attr("id");
            var sp = $(this).attr("sp");
            var mp = $(this).attr("mp");
            //var changedVal = $("#" + id).val();
            var changedVal = $("#"+this.id).val();
            if(changedVal < mp)
            {
                alert("Minimum Price of this Product is: " + mp);
                $("#" + id).focus();
                //return false;
            }
            else {
                alert("Price OK: " + changedVal + " mp:" + mp);
                //return true;
            }
            //alert("ind doc" + "\n" + "SP:" + sp + " " + "MP" + mp + "CV:" + changedVal);

        });
</script>

在 DataList 项模板中:

<p>
<asp:TextBox ID="txtSalesPrice"  class="numericText" sp='<%# Eval("SalesPrice") %>'
    mp='<%# Eval("MinimumPrice") %>' runat="server" Text='<%# Eval("SalesPrice") %>'
    Width="60px"></asp:TextBox><br />
<asp:Button ID="BtnDevices" runat="server" class="btnCart" Text="Add to Cart" CommandName="AddToCart"
    CommandArgument='<%# Eval("CategoryID") %>' />
</p>

注意: DataList 在 UpdatePanel 中。我还使用 ALERT 进行了检查,它显示的值正确,但我不知道为什么“LESSTHAN”符号无法评估?

不知道为什么?

有什么想法吗?

【问题讨论】:

    标签: jquery asp.net updatepanel conditional-statements


    【解决方案1】:

    试试这个

    if(parseInt(changedVal) < parseInt(mp))
    {
        alert("Minimum Price of this Product is: " + mp);
        $("#" + id).focus();
        //return false;
    }
    else {
        alert("Price OK: " + changedVal + " mp:" + mp);
        //return true;
    }
    

    【讨论】:

      【解决方案2】:

      parseInt 用于您的价值观。因为java脚本把这些值转换成字符串,所以条件出现了这个问题

      Javascript编译代码的样子

      if("10" < "9"))
          {
      
      //codes
      

      所以这些值是字符串,条件可能失败

      你需要把if条件改为

      if(parseInt(changedVal) < parseInt(mp))
      {
      // codes
      

      【讨论】:

      • 嘿@Ramesh Rajendran!是的,它工作正常..!谢谢:D
      • 哎呀,当我输入我的答案时,我确实看到了你的帖子。
      猜你喜欢
      • 2012-07-11
      • 2014-11-29
      • 2011-02-01
      • 2016-06-02
      • 2019-12-03
      • 2011-11-03
      • 2019-02-11
      相关资源
      最近更新 更多