我想为针对特定事件动态创建的文本框命名。 我编写了以下代码,其中函数 GenerateTextBox 返回文本框的名称和值“”。文本框由生成,但名称未分配。
这是使用名称作为对另一个 php 文件中文本框的引用。
生成文本框的jquery代码:
function GenerateTextbox(value,name1) {
return '<input name = "'+ name1 + '" type="text" value = "' + value + '" /> ';
}
调用函数:
$("#t11, #t12").click(function(){
var div = $("<div />");
div.html(GenerateTextbox("", c1));
$("#TextBoxContainer").append(div);
});
php 输出文件显示 c1 是未定义索引的错误...
我该如何解决这个问题?
最佳答案
将 c1
更改为 "c1"
。 c1
指的是名为 c1
的变量(您尚未定义),而 "c1"
指的是字符串。
div.html(GenerateTextbox("", "c1"));
工作代码:
function GenerateTextbox(value,name1) {
return '<input name = "'+ name1 + '" type="text" value = "' + value + '" />';
}
$("#t11, #t12").click(function(){
var div = $("<div>");
div.html(GenerateTextbox("", "c1"));
$("#TextBoxContainer").append(div);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="t11">Create Textbox</button>
<div id="TextBoxContainer"></div>
关于javascript - 如何通过 jquery 函数命名动态创建的文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52007378/