【问题标题】:extra input added by jquery doesn't workjquery 添加的额外输入不起作用
【发布时间】:2012-11-16 00:56:20
【问题描述】:

您好,我正在尝试使用 Jquery 添加动态输入,但是当我尝试获取代码时它不起作用

codeigniter 中的代码捕获所有 POST,但不包含 jquery 添加的那一个

代码如下:

<script type="text/javascript">
 $(document).ready(function(){
    var value = parseFloat($("#subtotal").val());
    $('.subtotal').html(value);
    $('#subtotal').val(value);
    $('input[name="phprop"]').change(function(event){
        if($('input[name="phprop"]:checked').val() == 'Yes'){
            $('#sections').show();
        }else if($('input[name="phprop"]:checked').val() == 'No'){ $('#sections').hide();}
        });
        $('input[name="xsst"]').change(function(event){
        if($('input[name="xsst"]:checked').val() == 'Yes'){
            $('#xtraphotos').show();
            $('<input type="text"  name="extraid'+i+'" id="extraid'+ i +'" style="width:80px;margin-left:4px;"/>').appendTo('#xtrabox');
            var num = value + 9.95;
            value = parseFloat(num.toFixed(2));
            $('.subtotal').html(value);
            $('#subtotal').val(value);
            $('#extrass').val(i);               i++;

        }else if($('input[name="xsst"]:checked').val() == 'No'){ 
            $('#xtraphotos').hide();
            $('#xtraphotos input[type="text"]').remove();
            var num = value - 9.95 * (i - 1);
            value = parseFloat(num.toFixed(2));
            $('.subtotal').html(value);
            $('#subtotal').val(value);
            $('#extrass').val(0);
            i = 1;
            }
        });
        var scntDiv = $('#xtrabox');
        var i = 1;

    $('#addScnt').live('click', function() {
            $('<input type="text"  name="extraid'+i+'" id="extraid'+ i +'" style="width:80px;margin-left:4px;"/>').appendTo(scntDiv);
            num = value + 9.95;
            value = parseFloat(num.toFixed(2));
            $('.subtotal').html(value);
            $('#subtotal').val(value);
            $('#extrass').val(i);
            i++;                
            return false;
      });       
 });
 </script>

这里是 HTML

 <div style="margin-left: 50px;">
<label><b>Do you need more photos for your web?</b> </label>Yes<input type="radio"     name="xsst" id="xsst" value="Yes"/>No<input type="radio" name="xsst" id="xsst" value="No"/> (Each aditional photo has a cost of <b>$9.95</b>)
    <div id="xtraphotos" style="display:none;"><a href="#" id="addScnt">Add another picture box</a><div id="xtrabox"></div></div></div> 

这是 Codeigniter 代码:

 if($_POST['extrass'] !=0){
        for($i=1;$i<=$_POST['extrass'];$i++){
            $names = "extraid".$i;
            $extras .= $_POST[$names];
            if($i!=$_POST['extrass']){
            $extras .= "-";
                }
        }

这是完整的 HTML(只是表单部分):

  <?php echo form_open_multipart(base_url() . 'purchase/confirmation')?>
    <p style="float:left">Image ID of the Photos for your Website</p><div style="float:left;margin: 13px 0 0 110px;width: 450px;"><?php 
   if($idtype == 4){$x=3;}elseif($idtype == 1){$x=5;}elseif($idtype == 2){$x=10;}elseif($idtype == 3){$x=13;}
    for($i=1;$i<=$x;$i++){?><input type="text"  name="ssid[]" id="ssid_<?=$i?>" value="" style="width:80px; margin-left:4px;"/><?php }?></div>
 </div>
  <div style="margin-left: 50px;">
  <label><b>Do you need more photos for your web?</b> </label>Yes<input type="radio"  name="xsst" id="xsst" value="Yes"/>No<input type="radio" name="xsst" id="xsst" value="No"/> (Each aditional photo has a cost of <b>$9.95</b>)
  <div id="xtraphotos" style="display:none;"><a href="#" id="addScnt">Add another picture box</a><div id="xtrabox"></div></div></div>


  <br />
    <div id="choose_your_template" style="background-image:url(<?=base_url()?>images/step_4.png)"><div style="padding-left:5px; float:left">
<span style=" position: relative; top: 14px; left: 210px; font-size: 18px; color: gray; ">Step 4: Do you have Photos? Upload the pictures for your website.</span></div>

    </div>
     <div style="margin-left: 50px;">
     <label>Do you have photos of your property? </label>Yes<input type="radio" name="phprop" id="phprop" value="Yes"/>No<input type="radio" name="phprop" id="phprop" value="No"/><br />
   <div id="sections" style="display:none;">
    <p>Upload the photos in the theme that they should be used. The photos should be in jpge.format. Otherwise the system will not accept. them.</p><br/>
   <?php 
    if($idtype == 4){$x=1;}elseif($idtype == 1){$x=3;}elseif($idtype == 2){$x=4;}elseif($idtype == 3){$x=5;}
   for($i=1;$i<=$x;$i++){?>
      <label>Section</label><select name="section[]" id="section"><option value="">Choose Section</option><option value="home">Home</option><option value="about-us">About Us</option><option value="contact-us">Contact Us</option></select><?php   for($j=1;$j<=4;$j++){?><input type="file"  name="userphoto<?=$i?><?=$j?>" /><?php }?><br />   <br />
  <?php }?>
     <p>I certify that the photos that i am uploading for the website development are of my entire property and that I have all the copyrights.</p></div>
   </div><br />


   <input type="hidden" id="domain" name="domain" value="<?=$domain?>" />
  <input type="hidden" id="payment_plan" name="payment_plan" value="<?=$poption?>" />
  <?php if($ownamedomain){?>
  <input type="hidden" name="ownamedomain" id="ownamedomain" value="<?=$ownamedomain?>" />
  <?php }?>
   <input type="hidden" name="idtype" id="idtype" value="<?=$idtype?>" />
  <input type="hidden" name="templateid" id="templateid" value="<?=$templateid?>" />
 <input type="hidden" name="extrass" id="extrass" value="0" />
 <input type="hidden" name="subtotal" id="subtotal" value="<?=$subtotal?>" />
 <div style="padding-left:45px; padding-top:15px;">
 <hr style=" width: 868px; margin: 0 0 16px; "/>
 <div align="center">
 <p>Subtotal: USD$<span class="subtotal"><?=$subtotal?></span> </p></div>
 </div>
 <div style="background:url(<?=base_url()?>images/bottombar.png) no-repeat;display: block;height: 16px;margin: 0 36px 10px;width: 902px;"></div>
 <input type="submit" name="continue" id="continue"/><input type="button" value="Back" onClick="history.back();" class="back">
  </div>

【问题讨论】:

  • 不工作是什么意思?是不是输入没有生成?或者动态生成的输入值没有被 codeigniter 捕获。
  • 输入动态生成的输入没有被codeigniter捕获,我知道为什么
  • 这里是codeigniter的代码 if($_POST['extrass'] !=0){ for($i=1;$i
  • 您能否编辑您的问题并包含您实际为动态生成的输入分配值的代码。我已经浏览了您的代码两次,您似乎没有为动态生成的输入分配一个值,我认为这是: ('').appendTo('#xtrabox')
  • 您确定 jQuery 代码将输入插入到正确的位置吗?另外,我很幸运地在 php 中调试了以下内容:&lt;?php echo print_r($_POST, true); exit; ?&gt; 应该回显您的所有帖子变量,以排除它成为您的 php 问题的可能性。

标签: php jquery forms codeigniter


【解决方案1】:

您的 HTML 有标签形式吗?

当您没有将表单元素放入表单时,onload,一些browser 添加表单以纠正代码语法...但是当您使用 DOM 添加 nes 元素时,可能这些元素不包含在此表单中...

【讨论】:

  • 什么意思?用另一种方式添加输入? PS。什么是“nes元素”?感谢回复
  • 我相信您正在创建的新元素没有被添加到表单中。检查 HTML 代码,在控制台尝试 $("form #extraid1"),这会返回一个对象?
  • 我做了,但它没有返回任何东西
  • 我的问题是对的,您需要在
    中添加输入元素
  • 它们在表单内,当我执行此操作 $("form #extraid1") 然后 $0
猜你喜欢
  • 2021-06-25
  • 2012-11-27
  • 2015-05-08
  • 2017-01-01
  • 2019-03-11
  • 2020-08-07
  • 2013-09-12
  • 2021-11-02
  • 1970-01-01
相关资源
最近更新 更多