如何在变量中获取类值?

时间:2017-01-25 05:13:49

标签: jquery html css

我有表的代码,在一个字段中我使用了class =" errorwidth"和其他是class ="错误"。我想在变量中获取这些类值。 &安培;这些类值在具有不同数值的表列上更改

<td class="error" style="color:red"></td><td class="errorwidth" style="color:red"></td> 

这里是我尝试获取课程数据的代码&#34; Cargo不适合20STD,40STD和40HC容器,将模式更改为批量&#34;在变量

var error = $(this).val();
  var errorOnly = ($(this).attr('class') == 'error') ? error : false;   

            var errorwidth = $(this).val();
  var errorwidthOnly = ($(this).attr('class') == 'errorwidth') ? errorwidth : false;    

                so that i can use that variables here in this code:


if (grandTotal > 7 && errorwidthOnly != "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk" && errorOnly != "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk") {
                    $('#result').html('FCL');
                } else if (grandTotal < 7 && errorwidthOnly != "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk" && errorOnly != "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk") {
                    $('#result').html('LCL');
                }
                else if((grandTotal > 7 || grandTotal < 7) && errorwidthOnly == "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk" || errorOnly == "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk" || errorOnly != "Cargo won't fit in 20STD and 40STD Containers, change the mode to bulk")
                {
                $('#result').html('BULK');
            }

1 个答案:

答案 0 :(得分:0)

您应该尝试使用text(),因为div始终包含文本作为值,因此val()在您的情况下不起作用。

var error = $(".error").text();// change here
  var errorOnly = ($(this).attr('class') == 'error') ? error : false;   

            var errorwidth = $(".errorwidth").text();//Change Here
  var errorwidthOnly = ($(this).attr('class') == 'errorwidth') ? errorwidth : false;    

                so that i can use that variables here in this code:


if (grandTotal > 7 && errorwidthOnly != "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk" && errorOnly != "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk") {
                    $('#result').html('FCL');
                } else if (grandTotal < 7 && errorwidthOnly != "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk" && errorOnly != "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk") {
                    $('#result').html('LCL');
                }
                else if((grandTotal > 7 || grandTotal < 7) && errorwidthOnly == "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk" || errorOnly == "Cargo won't fit in 20STD, 40STD and 40HC Containers, change the mode to bulk" || errorOnly != "Cargo won't fit in 20STD and 40STD Containers, change the mode to bulk")
                {
                $('#result').html('BULK');
            }