jquery split函数返回类型错误null值

时间:2014-08-26 07:53:12

标签: jquery html

我正在开发一个价格计算器表格。这是一篇文章/论文写作网站。网站链接是http://www.shopessays.com/order-now。请帮助我...我想使用jquery从值中选择具有数字值的选项中的值,然后我想拆分并在计算中使用...如果我不使用拆分那么我无法向客户显示计算出的价格,并且能够将订单表格的选定参数发送给管理员..请

<form method="post" action="" class="calc-form" enctype="multipart/form-data">
          <div>
            <label>Choose Currency</label>
            <select id="currency" class="shopEssaySelect" name="txtCurrency" required>
              <option selected disabled value="">Choose ...</option>
                <option value="1/$">US Dollar</option>
                <option value="3.65/Dirham">UAE Dirham</option>
            </select>
          </div>

          <div>
            <label>Type of Paper</label>
            <select id="category" class="shopEssaySelect" name="txtPaperType" required>
              <option selected disabled value="">Choose ...</option>
                <option value="10/Paper One">Paper One</option>
                <option value="10/Paper Two">Paper Two</option>
            </select>
          </div>
          <div>
            <label>Academic Level</label>
            <select id="type" class="shopEssaySelect" name="txtAcademicLevel" required>
              <option selected disabled value="">Choose ...</option>
                <option value="50/Profesional">Profesional</option>
                <option value="40/Graduate">Graduate</option>
            </select>
          </div>
          <div>
            <label>Deadline</label>
            <select id="deadline" class="shopEssaySelect" name="txtDeadline" required>
              <option selected disabled value="">Choose ...</option>
                <option value="345/12 Days">12 Days</option>
                <option value="568/3 Days">3 Days</option>
            </select>
          </div>
          <div>
            <label>Service Type</label>
            <select id="servicetype" class="shopEssaySelect" name="txtServiceType" required>
              <option selected disabled value="">Choose ...</option>
                <option value="12/New Work">New Work</option>
                <option value="4/Editing">Editing</option>
            </select>
          </div>
          <div>
            <label>Line Spacing</label>
            <select id="linespacing" class="shopEssaySelect" name="txtLineSpacing" required>
              <option selected disabled value="">Choose ...</option>
                <option value="12/250/Dopuble Space">Double Space</option>
                <option value="4/450/Single Space">Single Spacing</option>
            </select>
          </div>
            <div>
             <label>Paper Format</label>
             <div class="paper-format">
             &nbsp;&nbsp;<input type="radio" name="txtPaperFormat" value="MLA">MLA
             <input type="radio" name="txtPaperFormat" value="APA">APA
             <input type="radio" name="txtPaperFormat" value="Chicago">Chicago / Turabian
             <input type="radio" name="txtPaperFormat" value="Harvard">Harvard
             <input type="radio" name="txtPaperFormat" value="Other">Other
             </div>


          <div>
            <label>Paper Details</label>
            <textarea name="txtPaperDetail" required></textarea>
          </div>
          <div>
            <label>Attachment</label>
            <input type="file" name="fileatt" required />
          </div>
          <div>
            <label>Pages</label>

      <div class="numbers-row">

      <input name="numberOfPages" id="numberOfPages" class="shopEssaySelect" value="0" type="text" maxlength="3"  autocomplete="off" required>

    </div>

    <div class="word-count">Word Count<br />
      <span id="totalwords">0</span></div>
          </div>
                        <br clear="all" />
                        <p id="my-total">
                       Approximate Price: <span id="curr-symbol"></span>&nbsp;<span id="total">0</span></p>
                        <br clear="all" />
                        <br clear="all" />                            

          <div>
            <input type="submit" value="" class="sidebar-submitt-button-inner"/>
          </div>
      </div>     

    </form>

// jquery代码在这里

function updateTotal(){ 
    currency            = $("#currency").val();
    currency_array      = currency.split("/");
    currency_percentage = currency_array[0];
    currency_symbol     = currency_array[1];

    $("#curr-symbol").text(currency_symbol);    

    // from here split does not work
    paper_values        = $("#category").val();
    paper_array         = paper_values.split("/");
    paper               = paper_array[0];

    type_values         = $("#type").val();
    type_array          = type_values.split("/");
    type_percentage     = type_array[0];

    deadline_values         = $("#deadline").val();
    deadline_array          = deadline_values.split("/");
    deadline_percentage     = deadline_array[0];        

    service_values          = $("#servicetype").val();
    service_array           = service_values.split("/");
    service_percentage      = service_array[0];     

    // but from here split  works   
    spacing             = $("#linespacing").val();
    spacing_array       = spacing.split("/");
    spacing_percentage  = spacing_array[0];
    spacing_words       = spacing_array[1];

    type        = (paper * type_percentage) / 100;
    deadline    = (paper * deadline_percentage) / 100;
    servicetype = (paper * service_percentage) / 100;
    linespacing = (paper * spacing_percentage) / 100;

    currency = (paper * currency_percentage);               

    words   = Number($("#numberOfPages").val());

    counted_words = words * spacing_words;

    $("#totalwords").text(counted_words);

    total_amount = (paper + type + deadline + servicetype + linespacing + currency) * words;

    if (!isNaN(total_amount)){
            total_amount = Math.round((paper + type + deadline + servicetype + linespacing + currency) * words);
        }
        else {
            total_amount ="0";
        }

    $("#total").text(total_amount);
  }

   $(".shopEssaySelect").change(function(){
    updateTotal();
  })

  $("#numberOfPages").on("input", function(event) {
updateTotal();
      })

1 个答案:

答案 0 :(得分:1)

问题是你在变量中使用加号运算符+是变量而不是数值,所以你要连接它的值而不是添加它们。

尝试更改此内容:

total_amount = (paper + type + deadline +
                servicetype + linespacing + currency) * words;

if (!isNaN(total_amount)){
        total_amount = Math.round((paper + type + deadline + 
               servicetype + linespacing + currency) * words);
    }

对此:

total_amount = (Number(paper) + Number(type) + Number(deadline) 
                + Number(servicetype) + Number(linespacing)
                + Number(currency)) * words;

if (!isNaN(total_amount)){
        total_amount = Math.round(total_amount);
    }

小提琴演示:http://jsfiddle.net/p5cnyvx0/5/

相关问题