【问题标题】:jquery split function is not workingjquery拆分功能不起作用
【发布时间】:2014-08-26 05:38:09
【问题描述】:

我想从选择框中拆分值,因为我必须向客户显示总价,还要向管理员发送邮件。如果我不使用拆分值,那么我无法将选择选项标签发送给管理员以进行订单处理。我已经尽了最大的努力,但我失败了......我再次问这个问题......请在这件事上帮助我

<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();
      })

【问题讨论】:

  • 我们能知道您的意见吗?

标签: jquery split


【解决方案1】:

请尝试如下:

货币 = $("#currency").val(); currency_array = $.trim(currency).split('|');

【讨论】:

    【解决方案2】:

    如果要从选择框中选择选定的值,请执行以下操作:

    currency= $("#currency").find(':selected').val();
    currency_array = currency.split("/");
    

    试试上面的方法,我认为它适合你。

    【讨论】:

      【解决方案3】:

      试试这个fiddle:

      $(document).ready(function(){
                   $("#currency").change(function(){
                     changedDD($(this).val(), "currency");
                   });
                   $("#category").change(function(){
                     changedDD($(this).val(), "category");
                   });
                   $("#type").change(function(){
                     changedDD($(this).val(), "type");
                   });
                   $("#deadline").change(function(){
                       changedDD($(this).val(), "deadline");
                   });
                   $("#servicetype").change(function(){
                       changedDD($(this).val(), "servicetype");
                   });
                   $("#linespacing").change(function(){
                       changedDD($(this).val(), "linespacing");
                   });
      function changedDD(string, selectBox)
      {
          console.log("----------"+selectBox+"-----------");
          console.log(string.substring(0, string.lastIndexOf("/")) );
          console.log(string.substring(string.lastIndexOf("/") + 1, string.length));
      }
      
                    });
      

      【讨论】:

      • 我无法理解这段代码..我将如何在计算中使用这些值....因为我想通过添加或乘以变量来代表这些值计算总价格
      • 您检查过您的浏览器控制台吗?那是你所期望的吗?在 Windows 中检查 chrome 控制台:按 F12
      猜你喜欢
      • 2014-03-05
      • 2017-09-15
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 2013-05-10
      相关资源
      最近更新 更多