【问题标题】:Same name cannot be in different input types同名不能在不同的输入类型中
【发布时间】:2017-03-06 09:54:59
【问题描述】:

你好,我开发了一个表单,我通过单击添加新按钮来克隆一个 div。这个 div 有一个输入框和一个表格。当我单击 AddNew 按钮时,将创建一个新 div,它是第一个 div 的克隆,但我的问题相同,无法在这些 div 的输入框中输入。我无法实现此验证。

$("#insert17").click(function(){
$(".copyFromHere:first").clone().appendTo(".individualMarksSection")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
  
  
  
  <div class="portlet light portlet-fit box individual individualDepartmentSection"> 
   <input type="button" class="btn green individual" value="Add New+" id="insert17"></input>
	
                                <div class="copyFromHere portlet-body individual individualDepartmentSectionSub">
								<label class="label1 col-md-12 individual labelDepartmentName"> Enter Department Name </label>
								<input type="text" class="form-control individual DepartmentName"></input>
                                 <table id="tableboth" class="arrSubjects table table-striped table-hover individual">
                                 
    <thead>
        <th>Employee</th>
        <th>Salary</th>
        
    </thead>
    <tbody>
        <tr> 
            <td>
                <input type="text" class="form-control EmpName" disabled="true" name="EmpName">
            </td> 
            <td>
                <input type="text" class="form-control salary allownumericwithoutdecimal" maxlength="3" name="salary">
            </td> 
        </tr>
    </tbody>
</table>
</div>
	
</div>

【问题讨论】:

    标签: javascript jquery html css input


    【解决方案1】:

    只需在name="value[]"末尾添加方括号

    喜欢:

    <input type="text" class="form-control EmpName" disabled="true" name="EmpName[]">
    

    就像你将有一个empName 输入的array()。

    如果您在服务器端使用 PHP,您可以使用方括号语法将表单输入转换为数组,因此当您使用 name="EmpName[]" 时,您会得到一个数组:

    $EmpName = $_POST['EmpName']; // Returns an array
    print_r($EmpName); // Shows you all the values in the array
    

    这样,您可以添加任意数量的同名输入元素。

    【讨论】:

    • hum.. 使用 javascript/jquery 的操作
    • @Weedoze웃 很好,但他会将表单发送到服务器的某个地方,因此 PHP 只是举个例子。最主要的是使用方括号,如name="value[]",这将与 javascript/jquery 一起使用,实际上它只是正确使用 HTML
    • 如果你能给我一个工作小提琴,那就太好了。谢谢
    【解决方案2】:

    我在 javascript 中添加了验证脚本,在清除输入值后将添加克隆项目。 您可以随意更改代码。

        $(document).ready(function () {
            $("#insert17").click(function () {
                var cloned = $(".copyFromHere:first").clone();
    
                $(cloned).find(".EmpName:first").val("");
                $(cloned).find(".salary:first").val("");
                $(cloned).find(".DepartmentName:first").val("");
    
    
                cloned.insertAfter(".copyFromHere:last");
                checkDupes($(".EmpName"));
                checkDupes($(".salary"));
                checkDupes($(".DepartmentName"));
            });
    
            $(document).on('change', ".DepartmentName", function () {
                if (checkDupes($(".DepartmentName"))) {
                    alert('DepartmentName should not be same');
                }
            });
    
            function checkDupes(itemArray) {
                var isdupe = false;
                for (var i = itemArray.length - 1; i >= 0; i--) {
                    var value = itemArray[i].value;
                    if (value == null || value == '' || value.trim().length == 0) {
                        itemArray[i].style.backgroundColor = 'red';
                    } else {
                        if (i > 0) {
                            for (var j = i - 1; j > -1 && i > 0; j--) {
                                if (value.trim().toLowerCase() == itemArray[j].value.trim().toLowerCase()) {
                                    itemArray[i].style.backgroundColor = 'red';
                                    isdupe = true;
                                }
                            }
                            if (!isdupe) {
                                itemArray[i].style.backgroundColor = 'transparent';
                            }
                        }
                    }
                }
            }
    
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>  
      
      
      
      <div class="portlet light portlet-fit box individual individualDepartmentSection" style="display:none"> 
       <input type="button" class="btn green individual" value="Add New+" id="insert17"></input>
    	
                                    <div class="copyFromHere portlet-body individual individualDepartmentSectionSub">
    								<label class="label1 col-md-12 individual labelDepartmentName"> Enter Department Name </label>
    								<input type="text" class="form-control individual DepartmentName"></input>
                                     <table id="tableboth" class="arrSubjects table table-striped table-hover individual">
                                     
        <thead>
            <th>Employee</th>
            <th>Salary</th>
            
        </thead>
        <tbody>
            <tr> 
                <td>
                    <input type="text" class="form-control EmpName" disabled="true" name="EmpName">
                </td> 
                <td>
                    <input type="text" class="form-control salary allownumericwithoutdecimal" maxlength="3" name="salary">
                </td> 
            </tr>
        </tbody>
    </table>
    </div>
    	
    </div>

    【讨论】:

    • 这个回答没有帮助。我得到了相同的部门名称
    • @ArpitGupta 我以为你只想包含 Salary 和 EmployeeName,现在我更新了代码以包含所有三个输入,包括 DepartmentName。
    • 先生,我的问题是输入框输入的部门名称不能相同。当我点击 addnew 按钮时,输入框和表格的克隆会出现,但两个或两个以上输入框的(输入部门名称)值不能相同
    • 你可以调用'checkDupes($(".DepartmentName"));'部门名称更改方法 $(document).on('change', ".DepartmentName", function () { if (checkDupes($(".DepartmentName"))) { alert('DepartmentName 不应该相同'); } });这是所有三个输入的要求吗?
    • 我的线索较少,你能通过更新上面的小提琴来帮忙吗?这将是一件好事
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多