简体   繁体   English

不同的结果来自同一领域

[英]different result comes in same field

Here am using Dynamic-Form-Element-Creation-And-Deletion-Plugin-Addel to insert more values and its working but the problem am facing here is when i select a value from the select box used by addel the corresponding data is appearing in the corresponding box and if i choose another select box by clicking add button the corresponding value comes in the first field not in the corresponding field. 这是在使用Dynamic-Form-Element-Creation-And-Deletion-Plugin-Addel插入更多值及其工作方式,但是这里面临的问题是,当我从addel使用的选择框中选择一个值时,相应的数据就会出现在其中相应的框,如果我通过单击添加按钮选择另一个选择框,则相应的值将出现在第一个字段中而不是相应的字段中。 here is my view 这是我的看法

<div class="form-group ">
         <label class="col-lg-3 control-label">Select Tools<span
                            class="required">*</span></label>
            <div class="col-lg-6">

             <div class="addel">

                 <div class="form-group target">
                    <div class="input-group">
                        <div class="col-lg-6"> <select class="form-control tools" style="width: 100%" name="tool_id[]" id="tools" onchange="tool(this)"  required>
                        <option value="">Select</option>

                    </select></div>

                     <div class="col-lg-3">  
                  <input type="text" class="form-control" required name="quantity[]" id="quantity" placeholder="Quantity">
                  </div>
                  <div class="col-lg-3" id="avail">  
                  <input type="text" class="form-control" name="available" id="available" placeholder="Available" value="0 available">
                  </div>

                    <span class="input-group-btn">
                        <button type="button" class="btn btn-danger addel-delete"><i class="fa fa-remove"></i>
                        </button>
                    </span>
                </div>
            </div>

            <button type="button" class="btn btn-success addel-add"><i class="fa fa-plus"></i></button>
          </div>

        </div>
      </div> 

here is my script 这是我的剧本

<script>
function tool(sel)
 {

     var tools=sel.value;
     alert(tools);
     var url='<?php echo base_url(); ?>admin/tool/ajax_available';
      $.post(url, {tools:tools}, function(data)
      {  

              alert("Value: " + $("#avail").html(data));

      });

   }
 </script>

here is my controller 这是我的控制器

public function ajax_available()
{
    $data['available']=$this->Tool_model->view_available_by_type($_POST['tools']);
    //var_dump($data['available']);
    $this->load->view('admin/tool/ajax_available',$data);
}

here is my ajax_available view 这是我的ajax_available视图

<input type="text" class="form-control" name="available"  id="available" placeholder="" value="<?php echo $available->available;?> available">

my picture looks like this Valid XHTML http://spdc.in/demo/spectra/assets/images/s-tool.PNG . 我的图片如下所示: 有效的XHTML http://spdc.in/demo/spectra/assets/images/s-tool.PNG

You are not giving your elements unique identifiers. 您没有为元素提供唯一标识符。 Every time you add a new row with a dropdown and textbox, it generates the same IDs again for these new elements. 每次您添加带有下拉列表和文本框的新行时,它都会为这些新元素再次生成相同的ID。 This is invalid HTML, all elements must have a unique id. 这是无效的HTML,所有元素都必须具有唯一的ID。

So when you do 所以当你这样做

$("#avail").html(data)

it finds the first instance of an element with that ID - because there should only be one. 它找到具有该ID的元素的第一个实例-因为应该只有一个。 As far as javascript is concerned, the others don't exist, because they are not valid. 就javascript而言,其他都不存在,因为它们无效。

Since you will have multiple "Available" textboxes in the page, you can't rely on ID to populate the right one. 由于页面中将有多个“可用”文本框,因此您不能依靠ID来填充正确的文本框。 Give your "avail" div a class instead of an id, and then tell the script to find the related one by using its position in the markup: 给您的“可用” div一个类而不是一个id,然后告诉脚本通过使用其在标记中的位置来找到相关的一个:

<div class="col-lg-3 avail">
  <input type="text" class="form-control available" name="available" placeholder="Available" value="0 available">
</div>

And then in your code, instead of $("#avail").html(data) , do: 然后在您的代码中,而不是$("#avail").html(data)

$(this).parent().parent().find(".avail").html(data);

This goes up the DOM by two divs, to find the common ancestor of both the select ( this in the context of the function), and the div you want to change, and then searches within it for an element with the avail class, and populates it with the data. 这种上升的DOM由两个div,找到共同的祖先都选择( this在函数的上下文中),并且要更改,然后在div内它会搜索与元素avail类,以及用数据填充它。

You should also remove "id" attributes from any other elements which can be duplicated, such as "quantity", for example. 您还应该从任何其他可重复的元素中删除“ id”属性,例如“ quantity”。

PS I'm not sure why you use a textbox for this "Available" property - it seems like a status value coming from the database which the user should not change. PS我不确定为什么要为此“可用”属性使用文本框-好像状态值来自数据库,用户不应更改。 Therefore it should be a label or span, really. 因此,它实际上应该是标签或跨度。 Textboxes are for input, not output. 文本框用于输入,而不用于输出。 Plus your user could type and change the value, which might confuse them. 另外,您的用户可能会键入和更改值,这可能会使他们感到困惑。 But I will leave you to sort that. 但我会留给你排序。

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

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