简体   繁体   English

如何在jQuery中获取两个动态文本框值

[英]how to get two dynamic textbox value in jquery

Thanks in advance, Actually i have a form with two hidden textbox fields one is <input type="hidden" name="item_name" class="course_name" value="<?php echo $cartData['exam'];?>"> and the other is <input type="hidden" name="amount" class="amount" value="<?php echo $priceTotal;?>"> , the the value is taken inside the hidden field dynamically from PHP code through loop. 在此先感谢您,实际上我有一个带有两个隐藏文本框字段的表单,其中一个是<input type="hidden" name="item_name" class="course_name" value="<?php echo $cartData['exam'];?>"> ,另一个是<input type="hidden" name="amount" class="amount" value="<?php echo $priceTotal;?>"> ,该值是从PHP动态获取的通过循环的代码。 So how can i get the dynamic values of "item_name" textbox field and "amount" textbox field in comma seperated using Jquery when clicking the image button with id="placeOrder". 因此,当单击id =“ placeOrder”的图像按钮时,如何使用Jquery以逗号分隔获取“ item_name”文本框字段和“ amount”文本框字段的动态值。 For example like this : for amount-->200,300 and for course name -->PMP,CAPM . 例如这样的例子:金额-> 200,300,课程名称-> PMP,CAPM。 I have written some code it will take the values within the jquery each loop but i have to pass through ajax as json format like this data : {cname:course_name,priceBox:textboxVal} so value with comma seperated value should pass through course_name & textboxVal. 我已经编写了一些代码,它将在每个循环的jquery中获取值,但是我必须像这样的data : {cname:course_name,priceBox:textboxVal}一样通过ajax作为json格式传递data : {cname:course_name,priceBox:textboxVal}因此带有逗号分隔值的值应该通过course_name和textboxVal 。

My Page is 我的页面是

<html>
 <head>
  <title></title>
  <script>
    $(document).ready(function(){
      var myArray = [];
      $('.amount').each(function(){        

         var textboxVal = $(this).val(); 
         //alert(textboxVal);          

        });

       var myCourse = [];
         //dynamic course name
         $('.course_name').each(function(){

            var course_name = $(this).val();  
            //alert(course_name); 

           });

           if(textboxVal!="")
          {

                $.ajax({ 

                   type : "POST",     
                   url : "/invl_exams/cart",                  
                   cache : "false",
                   data :      {cname:course_name,priceBox:textboxVal},           
                   success : function(result){       

                     console.log(result);                                    

                   } 


              }); 


          }   


    });
  </script>
 </head>
</html>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">

         <td>            
            <input type="hidden" name="cmd" value="_xclick">
            <input type="hidden" name="business" value="shopbusiness@myshop.com">      
            <input type="hidden" name="upload" value="1">   
            <?php             

               if(isset($cartDatas)) 
                 { 
                   $itm_no = 1;
                   $amt = 0;                              
                   foreach($cartDatas as $key=> $cartData)  
                   {

                     $prices = $cartData['price'];     
                     $prd_price = ltrim($prices,'$');
                     $priceTotal = number_format((float)$prd_price, 2, '.', '');   


            ?>

              <input type="hidden" name="item_number" value="<?php echo $itm_no++;?>">        
              <input type="hidden" name="item_name" class="course_name" value="<?php echo $cartData['exam'];?>">     
              <input type="hidden" name="amount" class="amount" value="<?php echo $priceTotal;?>"> 
              <input type="hidden" name="shipping" value="shipping Address">  
              <input type="hidden" name="quantity" value="<?php echo $cartData['orders'];?>">      

            <?php                 

                  $price = ltrim($prices,'$');  
                  $orders = $cartData['orders'];              
                  $amt_Total = $price * $orders;
                  $amt += $amt_Total;
                  $amt_Total = number_format((float)$amt, 2, '.', ''); 

                  ///$amt_Total = round($price * floatval( $orders ),2); 


                }               

            ?>

              <input type="hidden" name="currency_code" value="USD">              
              <input type="hidden" name="amount" value="<?php echo $amt_Total;?>">                    
            <?php

              }

            ?>
            <input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but03.gif" name="submit" id="placeOrder">              
      </td>
      </form>

You can use jQuery and attribute selectors to get the value: 您可以使用jQuery和属性选择器获取值:

var item_name = $('input[name=\'item_name\']').val();
var amount = $('input[name=\'amount\']').val();

var result = item_name + ',' + amount;

Now you can put this in your click handler. 现在,您可以将其放入点击处理程序中。

You can do something like following: 您可以执行以下操作:

$(function(){
    var amount = [];
  var course = [];
    $('.amount').each(function(){ 
    amount.push($(this).val());
  });
  $('.course_name').each(function(){
    course.push($(this).val());
  });
  console.log(amount.join(',')); //comma seperated value
  console.log(course.join(',')) //comma seperated value
});

DEMO 演示

If your inputs are enclosed within form element you can use this serialize the form input values to json object and then pass it to ajax call. 如果您的输入包含在form元素中,则可以使用此方法将表单输入值序列化为json对象,然后将其传递给ajax调用。

var postData = $("#enclosing_form_elm").serializeObject();

and in your script add reference to serialize object function after jquery 并在脚本中添加引用以在jquery之后序列化对象功能

$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
    if (o[this.name] !== undefined) {
        if (!o[this.name].push) {
            o[this.name] = [o[this.name]];
        }
        o[this.name].push(this.value || '');
    } else {
        o[this.name] = this.value || '';
    }
});
return o;

}; };

Just a demo https://plnkr.co/edit/7maJAUSdakDuvVqXhnzi?p=preview 只是一个演示https://plnkr.co/edit/7maJAUSdakDuvVqXhnzi?p=preview

Thanks to Convert form data to JavaScript object with jQuery 感谢使用jQuery将表单数据转换为JavaScript对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM